Reputation: 438
If have a given file Ex:
abc def ghijk
And want to pass a pointer for the file to a function ex:
void myfunc(FILE * myfile)
How can I have the pointer in the file not point to the first char (in this case a) but instead to the first char after two spaces (in this case g)? For example id pass:
myfunc(myfile.charat(9)) //or something
then in myfunc() the first getc call would return 'g'.
Upvotes: 0
Views: 292
Reputation:
You're looking for the function fseek()
. Just write
fseek(myfile, 9, SEEK_SET);
before passing myfile
to your function. You might want to check the return value, so you know whether setting the position succeeded.
Btw, don't confuse the "file position indicator" (sometimes called "file pointer") with the "pointer to a FILE
". They are different things. The file position indicator is stored somewhere internally in FILE
(or, more likely, in some operating-system object associated with the FILE
) to know what position in the file to read or write next. FILE *
on the other hand is the memory location of your (stdio-internal) FILE
structure.
Upvotes: 2
Reputation: 206717
My suggestion will be to read and discard characters until you have encountered two space characters.
Write a function for that:
void skipUntilTwoSpaces(FILE* in)
{
int count = 2;
int c;
while ( count > 0 && (c = getc(in)) != EOF) )
{
if ( c == ' ')
{
--count;
}
}
}
and use it where it makes most sense in your code.
Upvotes: 2