Reaching EOF with fgets

I’m writing a function that perform some authentications actions. I have a file with all the user_id:password:flag couples structured like this: Users.txt user_123:a1b2:0 user_124:a2b1:1 user_125:a2b2:2 This is the code: How can I manage the EOF reaching? I saw that when EOF is reached fgets doesn’t edits anymore the usr_psw_line but neither returns a NULL pointer. … Read more

Representing EOF in C code?

EOF is not a character (in most modern operating systems). It is simply a condition that applies to a file stream when the end of the stream is reached. The confusion arises because a user may signal EOF for console input by typing a special character (e.g Control-D in Unix, Linux, et al), but this character is not … Read more

How to find out whether a file is at its `eof`?

fp.read() reads up to the end of the file, so after it’s successfully finished you know the file is at EOF; there’s no need to check. If it cannot reach EOF it will raise an exception. When reading a file in chunks rather than with read(), you know you’ve hit EOF when read returns less … Read more

How to detect EOF in Java?

It keeps running because it hasn’t encountered EOF. At end of stream: read() returns -1. read(byte[]) returns -1. read(byte[], int, int) returns -1. readLine() returns null. readXXX() for any other X throws EOFException. Scanner.hasNextXXX() returns false for any X. Scanner.nextXXX() throws NoSuchElementException for any X. Unless you’ve encountered one of these, your program hasn’t encountered … Read more

Java end of file

Task: Each line will contain a non-empty string. Read until EOF. For each line, print the line number followed by a single space and the line content. Sample Input: Sample Output:

How to use EOF to run through a text file in C?

How you detect EOF depends on what you’re using to read the stream: Check the result of the input call for the appropriate condition above, then call feof() to determine if the result was due to hitting EOF or some other error. Using fgets(): Using fscanf(): Using fgetc(): Using fread(): Note that the form is the same for all of them: … Read more

How does ifstream’s eof() work?

-1 is get‘s way of saying you’ve reached the end of file. Compare it using the std::char_traits<char>::eof() (or std::istream::traits_type::eof()) – avoid -1, it’s a magic number. (Although the other one is a bit verbose – you can always just call istream::eof) The EOF flag is only set once a read tries to read past the end of the file. If I … Read more

What is EOF in the C programming language?

On Linux systems and OS X, the character to input to cause an EOF is Ctrl–D. For Windows, it’s Ctrl–Z. Depending on the operating system, this character will only work if it’s the first character on a line, i.e. the first character after an Enter. Since console input is often line-oriented, the system may also not recognize the … Read more