How do I split a text file and store them in arrays to use for a template in C? -


i'm newbie @ c , i'm having problems splitting particular text file , storing tokens in array.

this txt file called data.txt- splitted "|":

public|jane|q|ms.|600|maple street|your town|iowa|12345 penner|fred|r|mr.|123|that street|winnipeg|mb|r3t 2n2 gardner|mark|e|mr.|76|the avenue|toronto|on|m5w 1e6 acton|hilda|p|mrs.|66|what blvd|winnipeg|mb|r3t 2n2 

my code :

#include <stdio.h> #include <string.h> #include <stdlib.h>  #define input_length 128 #define field_length 30 #define num_fields   9  int main( int argc, char *argv[] ) {   file *template = null;   file *data = null;    char input[input_length];   char customerdata[num_fields][field_length];   int  element = 0;   char *next;   char ch;    template = fopen( "template.txt", "r" );   if ( template != null )   {     // read in customers until we're done     data = fopen( "data.txt", "r" );     if ( data != null )     {        //next = strtok(data, "|");       while(fgets(input, input_length,data) != eof){         next = strtok(input, "|"); //splitting data stored in input |         while(next != null){           strcpy(customerdata[element],next);           //customerdata[element++] = next;           next =strtok(null, "|");           //element++;         }          //testing         for(element=0; element<input_length; element++){           printf("%s\n", customerdata[element]);         }         //printf("%s\n", input );        }          fclose( data );     }      fclose( template );   }    return exit_success; } 

doing give me error array type 'char [30]' not assignable , bunch of garbage output. need in knowing i'm doing wrong in splitting part.

there other stuff in code because main purpose of program split these files, store in array , use each position template: separate template.txt file:

welcome back, $1! hope , members of $0 family reminding neighbours there on $5 shop us. usual, ship order    $3 $1 $2. $0    $4 $5    $6, $7 $8 

for first token, output should like:

welcome back, jane! hope , members of public family reminding neighbors there on maple street shop us. usual, ship order     ms. jane q. public     600 maple street     town, iowa 12345 

the following code drops reference template.txt file not used in code.

an appropriate struct defined customer data

each field extracted input , copied array of customer data

the 'test' code outputs each field customer data array.

error checks implemented , appropriate action taken.

#include <stdio.h> #include <string.h> // strtok() #include <stdlib.h> // exit(), exit_failure  #define input_length   (128) #define field_length   (30) #define num_fields     (9) #define max_customers  (20)  struct customer {     char lastname[ field_length ];     char firstname[ field_length ];     char initial[ field_length ];     char salutation[ field_length ];     char streetnumber[ field_length ];     char streetname[ field_length ];     char cityname[ field_length ];     char statename[field_length ];     char zipcode[ field_length ]; };  int main( void ) {     struct customer customerdata[ max_customers ];      file *fp = null;      char input[input_length];      int  element = 0;     char *token = null;      // read in customers until we're done     if( null == (fp = fopen( "data.txt", "r" ) ) )     { // fopen failed         perror( "fopen read data.txt failed");         exit( exit_failure );     }      // implied else, fopen successful      while( element < max_customers && fgets(input, input_length, fp))     {         if( null != (token = strtok(input, "|") ) ) //splitting data stored in input |             strcpy( customerdata[element].lastname, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          if( null != (token = strtok( null, "|") ) )             strcpy( customerdata[element].firstname, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          if( null != (token = strtok( null, "|" ) ) )             strcpy( customerdata[element].initial, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          if( null != (token = strtok( null, "|" ) ) )             strcpy( customerdata[element].salutation, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          if( null != (token = strtok( null, "|" ) ) )             strcpy( customerdata[element].streetnumber, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          if( null != (token = strtok( null, "|" ) ) )             strcpy( customerdata[element].streetname, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          if( null != (token = strtok( null, "|" ) ) )             strcpy( customerdata[element].cityname, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          if( null != (token = strtok( null, "|" ) ) )             strcpy( customerdata[element].statename, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          if( null != (token = strtok( null, "|" ) ) )             strcpy( customerdata[element].zipcode, token );         else         { // invalid file format             fprintf( stderr, "line %d, contains invalid format\n", element);             fclose( fp );             exit( exit_failure );         }          element++;     } // end while      //testing #if 0     // code bug     for(int = 0; < element; i++)     {         printf("lastname:    %s\n", customerdata[element].lastname);         printf("firstname:   %s\n", customerdata[element].firstname);         printf("initial:     %s\n", customerdata[element].initial);         printf("salutation:  %s\n", customerdata[element].salutation);         printf("streenumber: %s\n", customerdata[element].streetnumber);         printf("streetname:  %s\n", customerdata[element].streetname);         printf("cityname:    %s\n", customerdata[element].cityname);         printf("statename:   %s\n", customerdata[element].statename);         printf("zipcode:     %s\n", customerdata[element].zipcode);     } #else     // corrected code     for(int = 0; < element; i++)     {         printf("lastname:    %s\n", customerdata[i].lastname);         printf("firstname:   %s\n", customerdata[i].firstname);         printf("initial:     %s\n", customerdata[i].initial);         printf("salutation:  %s\n", customerdata[i].salutation);         printf("streenumber: %s\n", customerdata[i].streetnumber);         printf("streetname:  %s\n", customerdata[i].streetname);         printf("cityname:    %s\n", customerdata[i].cityname);         printf("statename:   %s\n", customerdata[i].statename);         printf("zipcode:     %s\n", customerdata[i].zipcode);     } #endif       fclose( fp );      return exit_success; } // end function: main 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -