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 回圈的預期輸出片段:
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 回圈的輸出片段:
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/>
在我的 pre-U 學習 Python 之后,我剛剛開始使用 C,如果這里有人可以指導我了解我的代碼出了什么問題,我將非常感激。在檔案讀取程序中,我使用strtok對txt檔案中的行進行分解并存入list[i][k],如How to store tokens(strtok) in a pointer on an array 所示。它顯示了預期的值,但在下一個 for 回圈中,list[i][k]只顯示了最后一組值,如下圖所示。
uj5u.com熱心網友回復:
好吧,代碼有點亂,你只是想將你的檔案映射到一個二維陣列中。
有幾個麻煩:
if (count>0){
為什么 ?您希望陣列中的每一行都包含在內,不要跳過第一行。
list[count-1][count2]=p;
跳過-1。它與那里無關。
list[count-1][count2]=p;
是的,同一行中有兩個問題。
您在陣列中分配一個指向將更改的字串的指標。和指標別名。strtok 回傳一個指向實際字串的指標。它不會重新分配記憶體。
解決方案很簡單,就是將你的字串串起來,這樣它就有了一個新的記憶體,在下一個回圈周期中不會改變。
list[count][count2] = strdup(p);
不要忘記稍后釋放它。不要忘記檢查你的 strdup 是否沒有失敗:)
其他注意事項:您有未使用的變數。換行符保留在字串中的最后一個標記中。您可能想要洗掉它。
uj5u.com熱心網友回復:
strtok()回傳一個指向原始陣列的指標,該陣列將被下一次呼叫覆寫,fgets()因此您應該分配一個字串的副本。你可以使用strdup():
list[count][count2] = strdup(p);
另請注意,這while (!feof(file))不是測驗檔案中是否有另一行可用的可靠方法。您應該只呼叫fgets()并檢查回傳值:
while (fgets(line, 100, file)) { ...
這是一個修改后的版本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
/* count the number of lines */
while (fgets(line, sizeof line, file)) {
count ;
}
/* allocate the 2D array of tokens and the per line token counts 8/
char *list[count][5];
int listlen[count];
/* restart parsing at the beginning of the file */
fseek(file, 0, SEEK_SET);
count = 0;
while (fgets(line, sizeof line, file)) {
count2 = 0;
/* split the line on |, also stripping the trailing newline
* note however that strtok() does not allow for empty tokens */
for (char *p = strtok(line, "|\n"); count2 < 5 && p != NULL; p = strtok(NULL, "|\n")) {
list[count][count2] = strdup(p);
count2 ;
}
listlen[count] = count2;
count ;
}
for (int i = 0; i < count; i ) {
for (int k = 0; k < listlen[count]; k )
printf("%d %d %s\n", i, k, list[i][k]);
}
fclose(file);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/341522.html
標籤:C
下一篇:從指標中提取字符陣列長度的問題
