Structures and tokens

This is a discussion on Structures and tokens within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> typedef struct{ int id; char name[30]; char address[30]; char phone[20]; int pid; }Customer; int main ( void ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    2

    Structures and tokens

    Code:
    #include <stdio.h>
    typedef struct{
      int id;
      char name[30];
      char address[30];
      char phone[20];
      int pid;
    }Customer;
    int main ( void )
    {
       Customer buyer[1000];
    
       static const char filename[] = "c:/users/cyro/desktop/customer.txt";
       FILE *file = fopen ( filename, "r" );
       char *ptr;
    int i =0;
       if ( file != NULL )
       {
          char line [ 128 ]; /* or other suitable maximum line size */
          while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
          {
          //fputs ( line, stdout ); /* write the line */
          ptr = strtok(line," ");
            while(ptr !=NULL)
            {
                 //printf("%s \n",ptr);
                 char temp[30] ;
                 temp = &ptr;
                 fscanf("%d", &buyer[i].id);
                 strcpy(buyer[i].name, temp);
                 strcpy(buyer[i].address,temp);
                 strcpy(buyer[i].phone, temp);
                 fscanf("%d",&buyer[i].pid);
                 i++;
                 ptr = strtok(NULL," ");
            }
          }
          fclose ( file );
       }
       else
       {
          perror ( filename ); /* why didn't the file open? */
       }
       return 0;
    }
    Need help here. I am able to read a line of data, but I cannot break it up into tokens so that i can store them in an array.

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    109
    Quote Originally Posted by chetah View Post
    Code:
    char temp[30];
    temp = &ptr;
    You cannot overwrite address of a static array.

    Clearly you still have trouble understanding how pointers work, so drop strtok and simply use sscanf to read all elements in a line.
    Code:
    while (fgets(line, sizeof(line), file) != NULL) /* read a line */
    {
        int elements = sscanf(line, "%d %s %s %s %d", &buyer[i].id, buyer[i].name, buyer[i].address, buyer[i].phone, &buyer[i].pid);
        if (elements == 5)
        {
            // 5 elements read - line correct
        }
        else
        {
            // line not correct
        }
        ++i;
    }
    Last edited by DRK; 04-10-2013 at 12:52 AM.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    2
    Thanks very kindly for your response but it appears that a demiter of some sort is needed. when i try to read in my data into the array of structures, some of it is truncated. For example, my data is 3000 "Brian Robertson" "Rose Hall" "1784-34567", 1002. When i print the data having stored in an array of sturctures, it looks like this 3000 "Brian Robertson", "Rose 0 > part of the address is missing and the number is replace with 0. However, if I print one line of data, it prints perfectly. this is what my code looks like now.
    Code:
    #include <stdio.h>
    
    typedef struct{
      int id;
      char name[30];
      char address[50];
      char phone[20];
      int pid;
    }Customer;
    
    int getCustomer(FILE *, Customer[]);
    
    
    int main ( void )
    {
       Customer Buyer[1000];
    int elements;
    int n, j;
    
    
    
       static const char filename[] = "c:/users/cyro/desktop/customer.txt";
       FILE *file = fopen ( filename, "r" );
    
       if (file == NULL)
       {
           printf('file cannot open');
           exit(1);
       }
    
           int i =0;
           char line [ 128 ]; /* or other suitable maximum line size */
    
    
        int numRecords = getCustomer(file, Buyer);
    
     for ( n=0; n <numRecords; n++)printCustomer(Buyer[n]);
       printf("\n");
    
       //  fclose ( file );
    
       return 0;
    }
    
    int getCustomer(FILE * file, Customer list[])
    {
        int i=0;
        char line [150];
        int elements;
    
        while (fgets(line, sizeof(line), file) != NULL) /* read a line */
    
          {
            //printf("%s",line);
            elements = sscanf(line, "%d %s %s %s   %d", &list[i].id, list[i].name, list[i].address, list[i].phone, &list[i].pid);
            ++i;
          }
          return i;
    }
    
    void printCustomer(Customer c)
    {
             printf("%d   %s   %s   %s   %d\n",c.id, c.name, c.address, c.phone, c.pid);
    
    }
    1002 "Brian Robertson" "Rose Hall" "567-45455" 101
    1003 "Zeth Robertson" "Spring Village" "456-90987" 102
    1004 "Nichle Morris" "Rose Bank" "345-34233" 103
    1005 "Antonio Robertson" "Troumaca" "345-45455" 104
    Last edited by chetah; 04-10-2013 at 08:45 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    %s won't work with your data set, because it only reads until the first whitespace. But your names and addresses include whitespace.
    You could use %[ as a format specifier. Look up the manual page for sscanf(),

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by AndiPersti View Post
    %s won't work with your data set, because it only reads until the first whitespace. But your names and addresses include whitespace.
    You could use %[ as a format specifier. Look up the manual page for sscanf(),

    Bye, Andreas
    That's the reason. scanf() is quite clever, and effectively has its own little programming language built in. But it's not intuitive to do anything with it beyond the basics.
    If you do use scanf() you need to test the return to see how man fields are read.
    It's often easier to write your own ad hoc parser, particularly with quotes,
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tokens?
    By trenzterra in forum C++ Programming
    Replies: 12
    Last Post: 05-03-2005, 08:14 AM
  2. string tokens
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-14-2004, 09:54 AM
  3. fgets, tokens
    By bigzeppelin2k in forum C Programming
    Replies: 3
    Last Post: 01-22-2004, 04:37 PM
  4. Arrays and tokens
    By |PiD| in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 08:56 PM
  5. any use for tokens?
    By tetraflare in forum C++ Programming
    Replies: 1
    Last Post: 01-03-2003, 09:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21