I would like to know how to read input from a file that has this form:
lname,fname,city,state,phone.
I was thinking of using fscanf, except for that state could contain spaces and the different fields are separated by commas.
I would like to know how to read input from a file that has this form:
lname,fname,city,state,phone.
I was thinking of using fscanf, except for that state could contain spaces and the different fields are separated by commas.
Try reading all of the data in one string with fgets and then strtok it using the comma as the delimiting character. It's difficult to be more detailed since I have no idea what you intend to do with the data and how.
-Prelude
My best code is written with the delete key.
I want to load the data in and then display it some way like this:
How would I use strtok to achieve what I want?Code:main() { FILE *fp = fopen("phonebk.txt","r"); char lname[25],fname[25],city[25],state[25],phone[25]; char buffer[80]; fgets(buffer,80,fp); sscanf(buffer,"%s,%s,%s,%s,%s\n",lname,fname,city,state,phone); fclose(fp); printf("%s %s %s %s %s",lname,fname,city,state,phone); getch(); }
Code:main() { char lname[25],fname[25],city[25],state[25],phone[25]; char buffer[80] = "Howard,Moe,Brooklyn,New York,000-0000"; char *p; p = strtok(buffer,"'"); printf("%s %s %s %s %s",lname,fname,city,state,phone); getch(); }
Here's how you'd use strtok(), with comments.
Code:#include <stdio.h> #include <string.h> int main() { char lname[25],fname[25],city[25],state[25],phone[25]; char buffer[80] = "Howard,Moe,Brooklyn,New York,000-0000"; /* for the first call, pass strtok the buffer and the delimiter */ /* and remember to use strcpy() and not "=" */ strcpy(lname, strtok(buffer,","); /* for the rest of the calls, pass it NULL and the delimiter */ strcpy(fname, strtok( NULL, "," ); strcpy(city, strtok( NULL, "," ); strcpy(state, strtok( NULL, "," ); /* Change the delimiter to "\n" for the end of the line */ strcpy(phone, strtok( NULL, "\n" ); printf("%s %s %s %s %s",lname,fname,city,state,phone); getch(); return 0; }
That should be right, then again it's very late and I just got off work
p.s. I'm not sure if strtok() is in string.h or stdlib.h, you might double check.
Something like
Though for extra safety, you should use strncpy to prevent possible overflows, or make lname, fname the same length as buffer.Code:char *p; p = strtok(buffer,","); if ( p != NULL ) strcpy( lname, p ); p = strtok( NULL, ","); if ( p != NULL ) strcpy( fname, p ); // etc
> except for that state could contain spaces and the different fields are separated by commas.
This means you cannot use %s for the state
But you could do this
The "%[^,]" is like %s, except it means all chars except commaCode:if ( sscanf ( buffer, "%s,%s,%s,%[^,],%s", lname, fname, city, state, phone) == 5 ) { // good data } else { // bad data }
>strcpy(fname, strtok( NULL, "," );
Aside from forgetting the closing paren for strcpy, strtok returns NULL on failure. Neither of the arguments to strcpy can be NULL or the program may choke (unless you know that your implementation of strcpy can handle NULL arguments).
>I'm not sure if strtok() is in string.h or stdlib.h
string.h
-Prelude
My best code is written with the delete key.