1

When I add a file to a char array, then print, I get garbage output (random ASCII symbols). The file contains only text (a paragraph).

The code is as follows:

int arraySize = 0;
string line;
while(getline(inFile, line)){
    //cout << line << endl; // this will print array fine.
    arraySize += line.length();
}
char message[arraySize];
char encrypted[arraySize];
//adds file to array
int i = 0;
while(inFile.good() && !inFile.eof()){
    inFile.get(message[i]);
    i++;
}
message[i] = '\0';
//prints array
for(int i = 0; i < arraySize; i++){
    cout << message[i]; //this returns garbage values
}

I believe its printing garbage because it thinks there's nothing in the array messages, but I do not know why there is "nothing there."

anotherone
  • 548
  • 5
  • 21
King Twix
  • 52
  • 8
  • You aren't resetting the file between the two sets of reads, so you aren't getting any contents into `message`. You're printing the garbage that was left over when it was allocated. – Mark Ransom Nov 01 '17 at 21:05
  • @Ron adding the contents of the file to a char array. So if the file conatined "hello world" it would go to an array as {'h', 'e', 'l', 'l', 'o',. . . } – King Twix Nov 01 '17 at 21:07
  • @MarkRansom Ah... I had no idea that was required. Thank you! – King Twix Nov 01 '17 at 21:09
  • 1
    [why `while (!infile.eof())` is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Barmar Nov 01 '17 at 21:12
  • You are reading the all file into a string why do not you transform the string into an array instead of reading again? – Marco Nov 01 '17 at 21:12
  • @Barmar I did that trying to test something else, forgot to update. it should be "While(inFile)" – King Twix Nov 01 '17 at 21:15
  • That's still wrong. It should be `while(infile.get(message[i])))` – Barmar Nov 01 '17 at 21:16
  • You are using Variable Length Arrays, which are not part of the standard C++ language. Use dynamic memory (`char * message = new char[arraySize];`) or std::vector (`std::vector message(array_size);`) – Thomas Matthews Nov 01 '17 at 22:00

1 Answers1

1

The reason is you reached the end of file when you count the length of the text thus the read pointer is at the end of the file and you used it again to read the text file.

To do it: Get the read pointer again to the beginning:

inFile.clear();
inFile.seekg(0, ios::beg);
while(inFile.get(message[i])){
    i++;
}

Also don't use: while (!infile.eof()) it is considered to be incorrect.

  • What I recommend is to use std::vector you don not mind about the file size or any allocation / de-allocation of memory. So your code can be like this:

    std::ifstream inFile("data.txt"); // your file name here
    std::string strLine;
    std::vector<std::string> vecStr;
    
    while(std::getline(inFile, strLine))
        vecStr.push_back(strLine);
    
    for(int i(0); i < vecStr.size(); i++)
        std::cout << vecStr[i] << std::endl;
    
    inFile.close();
    

Have you seen how the code is charm above?

  • NB: You got the garbage values because the array is only declared but not initialized:

The first read gets the length of the text. But moved the read pointer to the end and then you did:

while(inFile.good() && !inFile.eof()){ // Will fail because inFile.eof() is true from the previous read.
    //std::cout << "Inside the reading loop" << std::endl;
    inFile.get(message[i]);
    i++;
}

As you can see above the loop will not be executed because the previous read reached the eof thus the array is just declared without being initialized thus as you know it contains garbage values.

To confirm that the loop is not executed un-comment the line above and see if the loop is executed. The result is no printing message which means it was not executed.

Raindrop7
  • 3,805
  • 3
  • 14
  • 27