0

I am trying to use fscanf to read a file. When fscanf hits a newline I would like it to do one thing and when it hits a space I would like to do something else. Is this hopefully possible?

char nam[100];
while (fscanf(pFile, "%s",  nam) !=EOF)
{
    if(space)
    //do something
    if(newline)
    //do something
}
3
Contributors
4
Replies
17
Views
4 Years
Discussion Span
Last Post by rubberman
0

Possible, yes. However, for clarity sake, I'd recommend following up your call to fscanf with fgetc and check the result for the character you want:

while (fscanf(pFile, "%99s", nam) == 1)
{
    int next = fgetc(pFile);

    if (next == ' ')
    {
        // Do something
    }
    else if (next == '\n')
    {
        // Do something
    }
}

Edited by deceptikon

0

Myself, when processing textual data I usually read a complete line, and then parse it for tokens of interest using string functions such as strstr(). I process lots of log files efficiently this way.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.