Hello, I am very new to C programming. Lets assume that the program creates a number of string separated by lines. Is it possible to fgets() the last line of the file? If yes, then how? Thank you very much in advance.
Hello, I am very new to C programming. Lets assume that the program creates a number of string separated by lines. Is it possible to fgets() the last line of the file? If yes, then how? Thank you very much in advance.
Yes, by reading one line at a time until you hit the end. The string you read just before that (you did remember to store that string somewhere, right?) is the last line.
If you're looking for a way to jump straight to the last line of a file, then no, not really. There's fseek, but it doesn't play well with text files, and walking backwards to find the newline can be difficult, depending on your OS and whether you care about portability. For Linux only, it's easy. For other OSes, not so much. There's a good explanation here: c - Using fseek to backtrack - Stack Overflow.
Thank you very much!