Using fscanf() to skip a string

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


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]);


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.



  • Trouble implementing a (stable) method of retrieving n bytes from file
  • Using fopen twice
  • Transfer from Server > Server > Client without download to server
  • My C program reads a text file. When I put it into Xcode it fails. What's happening?
  • Can't find my syntax error, VC++ says there's one
  • fscanf not reading floats correctly
  • OpenSSL::X509::Certificate Showing Certificate for Wrong Domain
  • Call C++ class member function from C (Visual Studio)
  • Reaping zombie process - child
  • Initializing a class using malloc
  • How to save dynamically created textboxes and their values
  • Detecting null parameter in preprocessor macro
  • pip in virtualenv gets ConnectTimeoutError
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Installed module is empty
  • Meteor helpers not available in Angular template
  • output of program is not same as passed argument
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Akka Routing: Reply's send to router ends up as dead letters
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Buffer size for converting unsigned long to string
  • SQL merge duplicate rows and join values that are different
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal