2

The input file is such that it has a string followed by an integer on first line and from second line it has a string followed by 2 integers. My below code works well but is there a way to skip the string ? I am just scanning it with some character array char sink[30]. Actually I don't need this value how can I use fscanf() to skip this string and just read integers.

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int v,i=0,f=1;
    static int *p,*q;
    FILE *fp;
    char sink[30];
    fp = fopen("some.txt","r");

    while(!feof(fp))
    {   
        if(f)
        {   
            fscanf(fp,"%s %d",sink,&v);
            p = (int *)malloc(sizeof(int)*v);
            q = (int *)malloc(sizeof(int)*v);
            f=0;
        }   
        else
        {   
            fscanf(fp,"%s %d %d",sink,&p[i],&q[i]);
            i++;
        }   
    }   

    fclose(fp);

    printf("The input vertices are\n");
    for(i=0;i<v;i++)
        printf("%d %d\n",p[i],q[i]);
    return 0;
}
dragosht
  • 3,237
  • 2
  • 23
  • 32
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    `fscanf(fp,"%*s %d",&v);` work well if your data is formatted as expected. It does _not_ reveal problems if your input is ill formed. `"%s"` and `"%d"` cause `scanf()` family to skip leading white-spaces, so now code has lost the ability to distinguish between `' '` and `'\n'`. To read "... on first _line_", recommend first reading the line, maybe with `fgets()` as suggested by @unwind and then scanning the resultant buffer. – chux - Reinstate Monica Oct 23 '14 at 14:43
  • @chux my understading about fgets() is it needs some predefined buffer and wouldn't it be a concern to use this? also again after doing fgets() we need to do sscanf() right? – Gopi Oct 23 '14 at 15:13
  • `fgets()` needs a predefined buffer. The buffer need not be excessively long as repeated calls could handle an excessive long initial "string". Or use `fgetc()`. IAC, not a big concern. `sscanf()` is 1 way to scan a buffer, `strtol()`, `strtok()` or simple code can parse the buffer. It comes down to the input error detection requirements. With this code would recommend at least simple tests: Check return values from `fscanf()` before using scanned values. Insure `v` is sane like (0 < v). BTW: usage of `feof(fp)` is wrong. Check return values of `fscanf()` instead and quit loop if `i==v`. – chux - Reinstate Monica Oct 23 '14 at 15:32
  • you can use the same fscanf() call for all the reading. Then, after the call check to assure that the return value from facanf() is 2, then nothing read for second string parameter, if 3 then both string parameters read. Then, act accordingly. – user3629249 Oct 24 '14 at 01:03

2 Answers2

9

For discarding data in scanf you use an asterisk in between the format specifier such as %*s , %*c etc. This is the same for fscanf . Simply add an asterisk to scan and discard the string:

fscanf(fp,"%*s %d",&v);

This will scan a string from the file,discard it and will then scan and assign an integer to v. You can do the same for your second fscanf:

fscanf(fp,"%*s %d %d",&p[i],&q[i]);
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

If your input is line-oriented, it's much better to use line-oriented input. Such as fgets(), which lets you read whole lines. Keeping track of whether or not the read line is the first or not is pretty easy, of course.

Also:

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Interesting read!! I always used to cast my malloc() so is it not required to cast malloc even while allocating memory to struct or any other data-types? I used to just do malloc beofre but later I started casting since I see many doing it – Gopi Oct 23 '14 at 13:30
  • You never need to cast to convert a `void *` to any other (non-function) pointer in C. So don't. – unwind Oct 23 '14 at 13:35