C language - Read specific data from text file

334 views Asked by At

I'm studying file I/O. I have a trouble in reading specific data.

text file :

index (x,y)

 1     2,3   1,5   8,2

 2     4,4

 3     0,1   9,4

 4

The number of (x,y) can be changed.

I read only numbers with following code:

while (1){

    getNum = fscanf(fp, "%d", &num);

    if (getNum == EOF)

        break;

    else if (getNum < 1)

        fscanf(fp, "%*[^0-9]");

    else

        printf("%d\t", num);

    }

How can I split index, x, y?

1

There are 1 answers

0
DrKoch On

Follow these steps:

  • Read the file line by line
  • Split a single line with space " " as delimiter into fields
  • starting at the second field (if present): split each field using comma "," as delimiter
  • convert each subfield from string to integer