仍處於C的擺弄階段不知道我在這裏做錯了什麼,但我認爲它與指針有關。無論如何。我試圖讀入並將製表符分隔的文本文件解析爲二維數組。實際的數組是6109 x 14,我分配了一個更大的指針數組。閱讀文本分隔文件C
FILE *in = fopen("afile","rt");
// read the first line from the file
char line[2000];
int x=0;
int y=0;
char *result[7000][14];
char *curResult;
char delim[]= "\t";
while (fgets(line, 2000, in) != NULL) {
printf("LINE IS %s\n",line);
curResult=NULL;
curResult = strtok(line,delim);
y=0;
while(curResult != NULL) {
result[x][y]= curResult;
curResult = strtok(NULL, delim);
y++;
}
x++;
}
//print a random line to check
for(int i=0; i<10; i++)
printf("%s\n", result[50][i]);
我打印在底部應該是第50行的行打印文本文件的最後一行。我用不同的數字而不是50來嘗試這個,但它們都打印相同的東西。
編輯:å文件看起來像這樣:
instance_id batch_id cmap_name INN1 concentration (M) duration (h) cell2 array3 perturbation_scan_id vehicle_scan_id4 scanner vehicle vendor catalog_number catalog_name
1 1 metformin INN 0.00001 6 MCF7 HG-U133A EC2003090503AA EC2003090502AA HP GeneArray Scanner medium Sigma-Aldrich D5035 "1,1-dimethylbiguanide hydrochloride"
2 1 metformin INN 0.00001 6 MCF7 HG-U133A EC2003090504AA EC2003090502AA HP GeneArray Scanner medium Sigma-Aldrich D5035 "1,1-dimethylbiguanide hydrochloride"
3 1 metformin INN 0.0000001 6 MCF7 HG-U133A EC2003090505AA EC2003090502AA HP GeneArray Scanner medium Sigma-Aldrich D5035 "1,1-dimethylbiguanide hydrochloride"
4 1 metformin INN 0.001 6 MCF7 HG-U133A EC2003090506AA EC2003090502AA HP GeneArray Scanner medium Sigma-Aldrich D5035 "1,1-dimethylbiguanide hydrochloride"
21 2 phenformin INN 0.00001 6 MCF7 HG-U133A EC2003091104AA EC2003091102AA HP GeneArray Scanner medium Sigma-Aldrich P7045 phenformin hydrochloride
22 2 phenyl biguanide 0.00001 6 MCF7 HG-U133A EC2003091105AA EC2003091102AA HP GeneArray Scanner medium Sigma-Aldrich P19906 1-phenylbiguanide hydrochloride
23 2 valproic acid INN 0.001 6 MCF7 HG-U133A EC2003091106AA EC2003091102AA HP GeneArray Scanner medium Sigma-Aldrich P4543 2-propylpentanoic acid
61 2a metformin INN 0.00001 6 MCF7 HG-U133A EC2003091103AA EC2003091102AA HP GeneArray Scanner medium Sigma-Aldrich D5035 "1,1-dimethylbiguanide hydrochloride"
你可以顯示「afile」的前幾行嗎? – 2012-04-17 17:54:43
我試過..im不知道如何在文本中格式化複製文件,並非所有這些新行實際上都是新行。我會看看我是否可以解決這個問題。 – JoshDG 2012-04-17 17:58:05
strtok返回指針。除非是令牌的副本,否則毫無意義。 – BLUEPIXY 2012-04-17 18:02:46