Reading Space Delimited Strings Line by Line and Storing Them in Arrays (in C) -


this question has answer here:

i have input file consisting of:

createhall "red-hall" "stardust" 24 20 createhall "orange-hall" "last_samurai" 10 20 

and want store each line in array future uses.

so far code is: (added lots of printf's debugging purposes.)

#include <stdio.h> #include <stdlib.h> struct str { char *commands[5]; };  struct str a[]; int main() {     int i=0;     int j=0;     char *token;     printf("starting program...\n");     char filename[] = "input.txt";     file *file = fopen ( filename, "r" );      if (file != null) {         char line [1000];         printf("read new line...\n");         while(fgets(line,sizeof line,file)!= null) /* read line file */ {             j=0;             printf("%s\n",line);             printf("start token stuff...\n");             /* first token */             token = strtok(line, " ");              /* walk through other tokens */             while( token != null )             {                 a[i].commands[j]=token;                 printf( "stored command : %s\n", token );                 token = strtok(null, " ");                 j++;             }             i++;         }          fclose(file);     }     else {         perror(filename); //print error message on stderr.     }     printf("finished processing tokens...\n\n\n");      printf("%s\n%s\t%s\t%s\t%s\n", a[0].commands[0], a[0].commands[1],a[0].commands[2],a[0].commands[3],a[0].commands[4]);     printf("%s\n%s\t%s\t%s\t%s\n", a[1].commands[0], a[1].commands[1],a[1].commands[2],a[1].commands[3],a[1].commands[4]);     return 0; } 

the output on console this:

starting program... read new line... createhall "red-hall" "stardust" 24 20  start token stuff... stored command : createhall stored command : "red-hall" stored command : "stardust" stored command : 24 stored command : 20  createhall "orange-hall" "last_samurai" 10 20 start token stuff... stored command : createhall stored command : "orange-hall" stored command : "last_samurai" stored command : 10 stored command : 20 finished processing tokens...   createhall "orange-hall"   l"      murai"  ai" createhall "orange-hall"   "last_samurai"  10      20 

i'm not c, (this small part of assignment) believe error in

a[i].commands[j]=token; 

line, since before reads words fine.

thanks in advance.

from the man

return value

the strtok() , strtok_r() functions return pointer next token, or null if there no more tokens.

so pointers storing addresses of char lib[1000], that, @ end of process, contain last line read.


take note defined bad array, without dimension:

struct str a[]; 

should be, @ least,

struct str a[2]; 

the fastest correction can be:

#define _bsd_source  #include <stdio.h> #include <stdlib.h> #include <string.h>  struct str { char *commands[5]; };  struct str a[2]; int main() {     int i=0;     int j=0;     char *token;     printf("starting program...\n");     char filename[] = "input.txt";     file *file = fopen ( filename, "r" );      if (file != null) {         char line [1000];         printf("read new line...\n");         while(fgets(line,sizeof line,file)!= null) /* read line file */ {             j=0;             printf("%s\n",line);             printf("start token stuff...\n");             /* first token */             token = strtok(line, " ");              /* walk through other tokens */             while( token != null )             {                 char *temp = strdup(token);                 if (temp != null)                 {                     a[i].commands[j]=temp;                 }                 else                 {                     fprintf(stderr, "no memory available store string\n");                     return 1;                 }                 printf( "stored command : %s\n", token );                 token = strtok(null, " ");                 j++;             }             i++;         }          fclose(file);     }     else {         perror(filename); //print error message on stderr.     }     printf("finished processing tokens...\n\n\n");      printf("%s\n%s\t%s\t%s\t%s\n", a[0].commands[0], a[0].commands[1],a[0].commands[2],a[0].commands[3],a[0].commands[4]);     printf("%s\n%s\t%s\t%s\t%s\n", a[1].commands[0], a[1].commands[1],a[1].commands[2],a[1].commands[3],a[1].commands[4]);     return 0; } 

in more complex project, duplicated strings must freed due heap allocation performed strdup.


another solution can implemented using malloc , allocating space each element , using strcpy copy token.

#include <stdio.h> #include <stdlib.h> #include <string.h>  struct str {     char *commands[5]; };  struct str a[2];  int main() {     int i=0;     int j=0;     char *token;     printf("starting program...\n");     char filename[] = "input.txt";     file *file = fopen ( filename, "r" );      if (file != null) {         char line [1000];         printf("read new line...\n");         while(fgets(line,sizeof line,file)!= null) /* read line file */ {             j=0;             printf("%s\n",line);             printf("start token stuff...\n");             /* first token */             token = strtok(line, " ");              /* walk through other tokens */             while( token != null )             {                 a[i].commands[j] = malloc(strlen(token)+1);                 if (a[i].commands[j] != null)                 {                     strcpy(a[i].commands[j], token);                 }                 else                 {                     fprintf(stderr, "no memory available store string\n");                     return 1;                 }                 printf( "stored command : %s\n", token );                 token = strtok(null, " ");                 j++;             }             i++;         }          fclose(file);     }     else {         perror(filename); //print error message on stderr.     }     printf("finished processing tokens...\n\n\n");      printf("%s\n%s\t%s\t%s\t%s\n", a[0].commands[0], a[0].commands[1],a[0].commands[2],a[0].commands[3],a[0].commands[4]);     printf("%s\n%s\t%s\t%s\t%s\n", a[1].commands[0], a[1].commands[1],a[1].commands[2],a[1].commands[3],a[1].commands[4]);     return 0; } 

same thing here, should freed mallocated memory.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -