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
}

Recommended Answers

All 4 Replies

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
    }
}

Is this a bad idea?

Is this a bad idea?

Can you be more specific?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.