c - Using fscanf() to skip a string -



c - Using fscanf() to skip a string -

the input file such has string followed integer on first line , sec line has string followed 2 integers. below code works there way skip string ? scanning character array char sink[30]. don't need value how can utilize fscanf() skip string , 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]); homecoming 0; }

for discarding info in scanf utilize asterisk in between format specifier such %*s , %*c etc. same fscanf . add together asterisk scan , discard string:

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

this scan string file,discard , scan , assign integer v. can same sec fscanf:

fscanf(fp,"%*s %d %d",&p[i],&q[i]);

c

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -