0

I am trying to read the first line of text from an input text file into a character array but I am stuck reading until the array is filled to MAX_CHAR.

while (j < MAX_CHAR && !InFile.eof() && crypt[j] != '\n')
   {
   InFile.get(crypt[j]);
   j++;
  }

I have tried a few different alternatives like setting boolean values to stop once a '\n' character is read, but nothing has worked. Also, later in the program, I have this code written:

 else if (decipher == '\n')
{
         cout << '\n';
         break;

decipher is set to the character array at a certain for loop value. When evaluated, the newlines are printed, so I know the file has the new line characters, but I do not know how to use them to end the array read.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 2
    `InFile.eof` and `InFile.get` don't look like C. But in C-like languages, you can't test for EOF before doing input. That is, you can't say "Am I at EOF? If not, read more input". You almost always want to say, "Try reading more input. Did I hit EOF while doing so? If not, use it." See also [Why is `while( !feof(file) )` always wrong?](https://stackoverflow.com/questions/5431941) – Steve Summit Nov 06 '22 at 17:35
  • Do not tag unrelated languages. This is not `C` code. – PaulMcKenzie Nov 06 '22 at 17:35
  • 3
    You check the value of `crypt[j]` first, and only then set that value. Anyway, `InFile.getline(crypt, MAX_CHAR)` does what you want out of the box. – Igor Tandetnik Nov 06 '22 at 17:36

0 Answers0