0
votes

I have a problem...

in attempting to parse a file seperated by nothing more than whitespace i have an issue... I have decided the best way to do this is to tokenise the string i have, so far i have put all my lines into an array (by defining all new entrys in the array via the newline character) So my array may contain 5 entrys as such : (each entry in the array defines the lines in the file)

1)mary  julia      anne                 steve
2)alex james              david        katie
3)omegle          yikes  craxy  horse
4)foo  bar       foobar    matt maximus
5)capital or     not       smack

As you can see, each entry in the file may contain differing amounts of undefined whitespace... which can be one or more tab spaces, or many regular space characters.

I've considered looping through the string char by char until non whitespace is detected, but this seems ugly...

any help?

Thanks :)

3
strtok() should be all you need. It ignores consecutive separators, so you won't get any empty stringsDave

3 Answers

0
votes

sscanf make it all for you:

char *s="\nmary  julia      anne     \t            steve", o[100];
int n=0;
while( sscanf(s+=n,"%99s%n",o,&n)==1 )
  puts(o);
0
votes
str += strspn(str, " \t\r\n" );
0
votes

use isspace()

From man isspace

isspace() checks for white-space characters. In the "C" and "POSIX" locales, these are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').