0

Below I have a snippet of my code. Basically, I am wondering why there is a difference in output when I individually print contextFile[0] and contextFile[1] versus through a for loop.

In contextfile.txt (which is the value of target_file_name in this case), I have the following:

hickory dickory dot had a little farm you feel me.

Here is the code:

cin >> target_file_name; ifstream fileExist(target_file_name); if (fileExist) { int count = 0; int contextSize = 1000; int keySize = 1000; char *contextFile; char *keyFile; contextFile = new char[contextSize]; keyFile = new char[keySize]; string command; fileExist >> contextFile[count]; while (!fileExist.fail()) { count++; fileExist >> contextFile[count]; } cout << "printing individual: " << contextFile[0] << contextFile[1]; cout << "Printing the contextfile array: " << endl; for (int i = 0; i < count; i++) { cout << contextFile[count]; } 

When I print individually, I get hi, which is the correct output.

When I print through the for loop, I just get straight ================.

Why is there a difference?

3 Answers 3

1

Because you print

cout << contextFile[count]; 

over and over again, instead of

cout << contextFile[i]; 

in your loop, resulting in undefined behavior, since contextFile[count] was never initialized.

Sign up to request clarification or add additional context in comments.

1 Comment

Just checked the code again and my understanding - there is an attempt to read the final contextFile[count], but on EOF it will - well, my reference doesn't say - probably be unset, but at least not a useful value, so my (now deleted) comment is wrong.
0

It's because you are printing only one value inside the for loop.

for (int i = 0; i < count; i++) { cout << contextFile[count]; } 

You need to make changes in that

for (int i = 0; i < count; i++) { cout << contextFile[i]; } 

Comments

0

Your input file has 50 characters in it. So, after your reading loop is finished, count is 50, and contextFile[0] up to and including contextFile[49] have been populated with data.

You are then outputting the first two individual characters of contextFile[], which is fine.

Then you are running a loop through the first count characters of contextFile[], which is also fine. But on every loop iteration, you are outputting contextFile[50], which has not been populated with any valid data. You should be outputting contextFile[i] instead:

for (int i = 0; i < count; i++) { //cout << contextFile[count]; std::cout << contextFile[i]; } 

That being said, I would suggest NOT using a char[] at all:

std::getline(std::cin, target_file_name); std::ifstream theFile(target_file_name); if (theFile) { std::vector<char> contextFile; char ch; theFile.seekg(0, std::ios_base::end); size_t size = theFile.tellg(); theFile.seekg(0, std::ios_base::beg); contextFile.reserve(size); while (theFile >> ch) { contextFile.push_back(ch); } /* alternatively: contextFile.reserve(size); std::copy( std::istream_iterator<char>(theFile), std::istream_iterator<char>(), std::back_inserter(contextFile) ); */ /* alternatively: contextFile.resize(size); theFile.read(&contextFile[0], size); size = theFile.gcount(); contextFile.resize(size); */ std::cout << "printing individual: " << contextFile[0] << contextFile[1]" << std::endl; std::cout << "Printing the contextfile array: " << std::endl; for (int i = 0; i < contextFile.size(); i++) { std::cout << contextFile[i]; } /* alternatively: for (std::vector<char>::iterator iter = contextFile.begin(); iter != contextFile.end(); ++iter) { std::cout << *iter; } */ /* alternatively: std::copy( contextFile.begin(), contextFile.end(), std::ostream_iterator<char>(std::cout) ); */ /* alternatively: std::cout.write(&contextFile[0], contextFile.size()); */ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.