Question

I would like to know if there is an equivalent of fseek and ftell when I'm working in main.

For example, if I type the name of a file when asked, at end I hit enter. Next I'll ask the user another file name, but there's a '\n' in the buffer that was not read. The user won't be able to type the name of the second file because the program will read the '\n'. So I would like to move one position forward in the buffer. Normally in a file I would do:

fseek(file, ftell + 1, SEEK_SET);

I would like to do the same thing when I'm in main, not working with a file.

Was it helpful?

Solution 2

Actually, it is possible to use fseek in main, you just have to set FILE * stream to stdin, so it would be:

fseek(stdin, ftell - 1, SEEK_SET);

OTHER TIPS

The easiest way to do this would be just to say

getc(stdin);

or even shorter

getchar();

and ignore the returned character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top