Ignoring whitespaces while reading input files in C -


i'm trying write code registers first word of every line command, want able read word regardless of there being spaces or not in front of it. i'm using fgets() , strncmp first x characters of each line this, doesn't work arbitrary amount of whitespace. have tried using sscanf() inside fgets() loop store first word of each line variable seems skipping through lines , reading them incorrectly. rather not post code quite lengthy this:

while( fgets(line, buffer, input) != null ) { if(strncmp(line, "word", 4) != null) //do } 

there many strncmps , each of them ignore arbitrary amount of preceding spaces.

you can use isspace skip on whitespace:

#include <ctype.h>  while( fgets(line, buffer, input) != null ) {   char *p = line;   while (isspace(*p)) // skip whitespace     p++;   if(strncmp(p, "word", 4) != null)   //do } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -