0

I try to read char by char from a file, however, it always give me one or even more new line character("\n") in the end.

The follow is my code:

ifstream file;
file.open(inputfile, ios::binary);

char c;
if(file.is_open()){
  while (!file.eof()) {
      file.get(c);
      if(file.eof()) break;
      cout << c << endl;
  }
}
file.close();

My file is just one line txt file:

1122aaabbbcccc***

No new line character in the end. However the output of my code is:

1
1
2
2
a
a
a
b
b
b
c
c
c
c
*
*
*


Program ended with exit code: 0

Notice there is two new line character in the end. How can I fix this?

Huaxin Zhang
  • 53
  • 1
  • 8
  • 1
    Whomever prints "Program ended with exit code: 0", could it be that this guy also adds a couple of newlines? – Ivan Aksamentov - Drop Feb 22 '18 at 02:53
  • Otherwise, check what are the actual "newline" chars being printed: open the file in text editor, Ctrl+A, then Ctrl+C, then paste [here](https://www.browserling.com/tools/text-to-ascii). Or print chars as ints. I suspect either CRLF or some unicode points or... – Ivan Aksamentov - Drop Feb 22 '18 at 02:58
  • 2
    You can shorten your loop to the idiomatic `while(file.get(c)) { cout << c << endl; }` and forget about checking for `eof()`. – Galik Feb 22 '18 at 02:59
  • 2
    Print the character codes, via `cout << +c` or `cout << (int)c` – Ben Voigt Feb 22 '18 at 03:01
  • I cannot reproduce this. Try outputting it to a file and looking at the raw output, not from an IDE. It looks like the extra newline was added in to separate the program output from the "Program ended" message. Your program printed one newline and the IDE added another one resulting in two. – eesiraed Feb 22 '18 at 03:29

3 Answers3

0

Running the code you posted with the given input yields the exact same results for me. Taking your input and converting it to ASCII code gives the following output

49 49 50 50 97 97 97 98 98 98 99 99 99 99 42 42 42 10 10 

The last two 10 are the newline characters that are being printed out

Iliketoproveit
  • 445
  • 6
  • 15
0

Copying your program and having it output to a file results in exactly what you want with only one newline.

To me, it looks like the extra newline was added in by your IDE to separate the program output from the message telling you the program as ended.

Try outputting to a file or print some output without an ending newline to see if that is the case.

eesiraed
  • 4,626
  • 4
  • 16
  • 34
-1

cout << c << endl; well, you are printing new lines (std::endl inserts a new line character specific to the current OS). So it isn't surprising that there are new line characters in the output. Remove the << endl part of that line and you will get exactly what you want :)

Edit: Just noticed that you only what to remove the two new line chars at the end. In this case: The line "Program ended with exit code: 0", as well as the two new line chars before that are not printed by your program at all (or at least not from what you've posted here) - I've even tested your code and it prints exactly what you want in my case.

If it's printed by whatever executes your program (for example your IDE), you can't really do anything about that, except running without that program. However, if it is caused by something inside your code that you haven't posted here, you can only fix it by changing that part of your code. There is now way you could prevent something to be printed without preventing that specific call to cout (or whatever is used to print a that text) in the first place.

Bizzarrus
  • 1,194
  • 6
  • 10
  • His problem is that there are two newlines. – eesiraed Feb 22 '18 at 03:22
  • Sure, if the file ends with a `'\n'` -- you get 2. If the file is a POSIX compliant text file opened in binary -- you would expect a final `'\n'`. – David C. Rankin Feb 22 '18 at 03:26
  • @FeiXiang Thanks, I've noticed that just the moment after I've posted the answer, just needed a moment to edit it xD In this case, the second new line character shouldn't be there, except his IDE or whatever prints the last line inserts one by itself. I've tested it and there are no empty lines for me. – Bizzarrus Feb 22 '18 at 03:34
  • I figured the exact same. – eesiraed Feb 22 '18 at 03:35