How can I make fgets stop reading when it reaches a new line? Right now it will read the new line and continue until the buffer is full.
I was thinking something like this.
Code:while(fp!='\n'){ fgets(password, 256, fp); }
How can I make fgets stop reading when it reaches a new line? Right now it will read the new line and continue until the buffer is full.
I was thinking something like this.
Code:while(fp!='\n'){ fgets(password, 256, fp); }
You should not be comparing a FILE pointer with '\n'. It should be:
since fgets returns a null pointer when end of file (with nothing read) or an error is detected.Code:while (fgets(password, 256, fp)) { /* ... */ }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
A single call to "fgets()" does stop reading when it reaches a newline (unless the maximum specified number of characters has been read first, or EOF is encountered) - see here. Note that the newline is stored as part of the string, assuming there is room.How can I make fgets stop reading when it reaches a new line?
So you can check the string for the presence of the newline to see if the entire line has been read.
See here for more information and an example: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com