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
Post a Comment