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

Difference between fgets and fscanf?

The function fgets read until a newline (and also stores it). fscanf with the %s specifier reads until any blank space and doesn’t store it… As a side note, you’re not specifying the size of the buffer in scanf and it’s unsafe. Try:

Difference between scanf() and fgets()

There are multiple differences. Two crucial ones are: fgets() can read from any open file, but scanf() only reads standard input. fgets() reads ‘a line of text’ from a file; scanf() can be used for that but also handles conversions from string to built in numeric types. Many people will use fgets() to read a line of data and then use sscanf() to dissect it.

Why do I get an assertion failure?

My guess is that the file is failing to open, and you’re still passing it to fgets. Your if(rd==NULL) doesn’t stop execution of the fgets if it’s null, it just prints out a message and continues with execution. Some very basic errorr handling: