I have a text file which contains lines, such that every line contains no more than 80 chars. every line contains (one or more words) divided by commas or spaces. I need lexicography sort the words. I want to use scanf with the symbol “%s” of strings, but it considers only spaces. So I guess I can’t. any smarter way to deal with the parsing the words than get char by char?
Here’s a sketch of what I was planning to do:
char**arr;
arr=calloc(Size, sizeof(char)*80);
int m=0;
while (!feof(file)) {
char c=fgetc(file);
while (c!='/n') {
j=0;
char* current;
current=calloc(1,sizeof(char)*80);
while (c!=','&& c!=' ' && c!='/n') {
current[j]=c;
j++;
c=fgetc(c);
}
current[j]='\0';
arr[m]=current;
free(current);
}
}
Assuming you have an entire line in a suitable variable (
line, below), you could use something like:This uses the (rather under-utilized, in my opinion) character group format
%[]to grab characters until a comma or space is found, then skip past the parsed token, and any separators that follow.