Search code examples
cfgets

Is there a way to move back a line after using fgets?


Is there a way to move back the line just read after calling fgets? I know when you use fgetc you can do ungetc. Is there a way I can do something similar for fgets?

The reason I am asking this is because I have an fgets in an if statment and I dont want the fgets to be actually executed I just want to know whether it is going to return a NULL or not. But I know that if the fgets is within an if statement it will be executed. This is why I want to know how can I move it back to the point in the file before fgets was called.


Solution

  • There is no fungets.

    You can save a position in an input stream using ftell, and reposition to that point using fseek. It is useful for files, but not reading from the terminal (i.e., the standard input).

    Further reading