1

I am wondering what is causing this strange output from simply reading a text file using ifstream. I can save a file with my text editor, and Notepad will open it correctly, my text editor, on the other hand, opens the text file along with weird unicode characters. (e.g. these images:)

enter image description hereenter image description here

The text editor on the left, Notepad, has no issues opening a text file. The text editor on the right, my text editor, seems to have appended a weird unicode character.

I am wondering if fstream is the issue, or perhaps my openTextFile() method is buggy. I am not exactly sure why this is happening, I suspect it could be just my values within my code. Here is the mentioned code below (by the way, I am using the Win32 API):

This code is within the openTextFile() method.

char buffer[1024];
int count = 0;
ifstream file("path",ifstream::in);
while(file.good()){
    buffer[count]=file.get();
    count+=1;
}
file.close();

Feedback would be very appreciated. Thanks.

-CA1K

  • Sorry, but bug in your code vs bug in notepad - I know which one I'm pointing my finger at... (doesn't mean I'm right, but in this case I'd confidently put money on it ;-) - 1st place to look: Does the buffer need to be 0 terminated? is it? – John3136 Nov 18 '15 at 01:15
  • Its not zero terminated. – CA1K Productions Nov 18 '15 at 01:17
  • Second place to look: probably the last call to `get()` is returning `EOF` during the last iteration of your loop. As hinted by the question linked by @user657267, your loop is structured wrong. – roeland Nov 18 '15 at 01:20
  • Odds are pretty good that if you are printing to the console you want the zero terminator, not EOF. – user4581301 Nov 18 '15 at 01:34

0 Answers0