C File Parsing Issue -
i have file read in in format like
3%6%1
5%3%0
4%9%2
i need in format can save separate fields each line, suppose can make typedef something.num1 = 3, something.num2 = 6, something.num3 = 1
here's have far:
#define buf 128 #define lines 100 char line[lines][buf]; file *input = null; int = 0; int total = 0; input = fopen("input.txt", "r"); while(fgets(line[i], buf, input)) { /* rid of ending \n fgets */ line[i][strlen(line[i]) - 1] = '\0'; i++; } total = i; printf("original read:\n"); for(i = 0; < total; ++i) { printf("%s\n", line[i]); } printf("\nparsed:\n"); char *token; char parsed[lines][buf]; for(i=0; i<total; i++) { token = strtok(line[i], "%"); while(token != null) { strcpy(parsed[i],token); token = strtok(null, "%"); } } for(i=0; i<total; i++) { printf("%s\n",parsed[i]); }
the problem when print out values in parsed array, seems have last value of each line, (ie sample ^ output 1,0,2). i'm new c programming, how can go this?
right using i
index parsed numbers, i
line index. need separate index keep track of numbers have parsed.
int numbercount = 0; ... strcpy(parsed[numbercount++],token); ... for(i=0; i<numbercount; i++) printf("%s\n",parsed[i]);
Comments
Post a Comment