Hi,
I am trying to write a program which read a certain input in a particular format and then gives an ouput. I wrote a code to read the file. Input is something like this.
X 20 60 80 90
X 78 98 97 96
X 34 35 43 23
Y 76 87 34
Y 65 76 72
Z 13 70 39
So my reading function read the input and then it counts the number lines with X and Y. Now, when I wrote two different funcions to read X and Y lines. When I am finished reading the X lines, I go to read Y lines. The code skips the first Y line, because it has already read it without realising that it is a Y line, and then it read the next line while skipping the first Y line. Can I avoid that?

I use scanf to read the first character and then it is true I used scanf in a while loop to read the rest of the numbers and I store those numbers in an array.

As I know that it has already read the next character, can I test it if the character is Y? Because the input may not contain the Y line.

Thanks.

I would, first off, recommend against using scanf() directly for this; since the input is by line, and of irregular size, I would instead read the data with fgets() and then apply sscanf() to the string after it has been read in. This should avoid the issue nicely, as you need only check to see if there is more data in the string on each pass of the loop, rather than having to check for a specific marker.

Thanks Schol-R-LEA. I am new to C. I have only been taught scanf and getchar.

Here's an example of how to inspect the first character of a "word" and respond to it's type (either a digit or not). Note that this program doesn't do what your requirements state, but it can be used as a template. So don't complain that I didn't solve your exact problem, thanks.

#include <ctype.h>
#include <stdio.h>

int main(void)
{
    FILE *in = fopen("test.txt", "r");

    if (in)
    {
        char lead, header = '\0';
        int value, n = 0;

        while (true)
        {
            int rc = fscanf(in, " %c", &lead);

            if (rc != 1 || !isdigit(lead))
            {
                if (n != 0)
                {
                    printf("Found %d values on '%c' line\n", n, header);
                }

                // End-of-file was reached, but we still wanted to print the last line result
                if (rc != 1)
                {
                    break;
                }

                header = lead; // Save the lead for later display
                n = 0;         // Reset the value count for a new line
            }
            else
            {
                // Return the lead character because it's part of a value
                ungetc(lead, in);

                // Extract the whole value, but discard it
                if (fscanf(in, "%d", &value) == 1)
                {
                    ++n;
                }
            }
        }

        fclose(in);
    }

    return 0;
}

The reason I posted this code is because there's a little bit of trickiness in three cases:

  1. Making sure not to print bogus values on the first line.
  2. Being able to locate the start of a new line.
  3. Not having the last line get lost.

See if you can figure out what I did to handle each case and why.

Edited 3 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.