0

I need to read the following format of text:

text
textEOF

Where EOF is a single CTRL+D and the rest are just the characters (\n is important).

The only way it does work is by adding another EOF after the first one, which I cannot do.

text
textEOFEOF

So I tried to use getchar(), but it seems to malfunction in some way or I am using it incorrectly. This is my code. What am I missing here?

    string a;
    int current;

    do {
        current = getchar();
        if(current != EOF) a.push_back((char)current);
    } while ( current != EOF );

    cout << a;
Purple6
  • 19
  • 3
  • 2
    Please provide a [mre]. I can't compile your code. – Thomas Weller Apr 02 '23 at 16:44
  • Are you actually putting a `EOF` control character in the file contents? That's a character that's content - it does not mean "end of file". – Andrew Henle Apr 02 '23 at 16:50
  • `ctrl-D` is not the same as EOF. EOF means, literally, the end of the file, i.e. there is nothing more to be read from the input stream. Your code works fine if you provide it with a file that just "ends" after the second `text`. [See this previous answer for discussion of ctrl-D / EOF](https://stackoverflow.com/a/21261742/21146235). – motto Apr 02 '23 at 16:51

0 Answers0