int main(){
FILE *file;
char line[100];
char name[26],code[4],donator[10],shipment[10], quantity[10];
int count = 0;
file = fopen("donation.txt","r");
if(!file){
printf("File does not exist!");
return 1;
}
while (!feof(file)){
fgets(line,100,file);
count++;
}
char *list[count][5];
memset(list,0,sizeof(list));
fseek(file,0,SEEK_SET);
count=0;
int count2=0;
char dtm[sizeof(line)];
while (!feof(file)){
fgets(line,100,file);
if (count>0){
strcpy(dtm,line);
printf("%s",dtm);
count2=0;
for(char *p = strtok(dtm,"|");p ; p=strtok(NULL,"|")){
printf("\n %d %s",count2,p);
list[count-1][count2]=p;
printf("\n%s",list1[count-1][count2]);
count2++;
}
}
count++;
}
for(int i =0; i<count-1 ;i++){
for(int k=0;k<count2;k++)
printf("\n%d %d %s",i,k,list[i][k]);
}
fclose(file);
return 0;
}
.
Contactless Thermommeter | CT | Japan | 1 | 1
Hand Sanitizer | HS | USA | 1 | 1
Face Mask | FM | China | 1 | 1
Surgical Mask | SM | China | 1 | 1
Oxygen Mask | OM | Saudi Arabia | 1 | 1
for loop's expected output snippet:
0 0 Contactless Thermometer<br/>
0 1 CT<br/>
0 2 Japan<br/>
0 3 1<br/>
0 4 1<br/>
1 0 Hand Sanitizer<br/>
1 1 HS<br/>
1 2 USA<br/>
1 3 1<br/>
1 4 1<br/>
for loop's output snippet:
0 0 Oxygen Mask<br/>
0 1 OM<br/>
0 2 Saudi Arabia<br/>
0 3 1<br/>
0 4 1<br/>
1 0 Oxygen Mask<br/>
1 1 OM<br/>
1 2 Saudi Arabia<br/>
1 3 1<br/>
1 4 1<br/>
I just started C after learning Python in my pre-U and I am very grateful if anyone here can guide me on what went wrong with my code. In the file reading process, I used strtok to break down the lines in the txt file and store in list[i][k]
, as shown in How to store tokens(strtok) in a pointer on an array. It shows the intended value but in the next for loop, list[i][k]
only showed the last set of values as the picture below.