Thread: reading delimited input from file

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    reading delimited input from file

    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.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    I want to load the data in and then display it some way like this:

    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();
    }
    How would I use strtok to achieve what I want?

    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();
    }

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like

    Code:
    char *p;
    
    p = strtok(buffer,",");
    if ( p != NULL ) strcpy( lname, p );
    p = strtok( NULL, ",");
    if ( p != NULL ) strcpy( fname, p );
    // etc
    Though for extra safety, you should use strncpy to prevent possible overflows, or make lname, fname the same length as buffer.

    > 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
    Code:
    if ( sscanf ( buffer, "%s,%s,%s,%[^,],%s", lname, fname, city, state, phone) == 5 ) {
        // good data
    } else {
        // bad data
    }
    The "%[^,]" is like %s, except it means all chars except comma

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. C++ Input File Reading Help!
    By beast0000 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2009, 10:12 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM