[英]Read in text delimited file C
Still in the fiddling stage with C. Not sure what I'm doing wrong here but I think it has to do with pointers. 仍处于C的摆弄阶段。不确定我在这里做错了什么,但我认为它与指针有关。 Anyway.
无论如何。 I'm trying to read in and parse a tab-delimited text file into a 2D array.
我正在尝试读取并将制表符分隔的文本文件解析为2D数组。 The actual array is 6109 x 14, I allotted a bigger pointer array.
实际的数组是6109 x 14,我分配了一个更大的指针数组。
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]);
The line I printed at the bottom which should be the 50th line prints the last line of the text file. 我在底部打印的行应该是第50行,将打印文本文件的最后一行。 I tried this with different numbers instead of 50 but they all print the same thing.
我用不同的数字(而不是50)尝试了此方法,但是它们都打印相同的内容。
EDIT: afile looks like this: 编辑:一个文件看起来像这样:
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"
Imagine line
is a big box where you can put a bunch of letters. 想象一下,
line
是一个大盒子,您可以在其中放一些字母。
That's what you do with fgets()
. 那就是你对
fgets()
所做的。
Next you point to specific letters inside that box. 接下来,您指向该框中的特定字母。
Next you replace the contents of the box (another fgets()
to the same variable line
) and the pointers, of course still point to the same box, but now to the new letters. 接下来,替换框的内容(另一个
fgets()
到同一变量line
)和指针,当然仍然指向同一框,但是现在指向新字母。
You need to copy the characters (with strcpy()
, check that you have enough space first ) somewhere inside your loop. 您需要在循环内的某个位置复制字符(使用
strcpy()
,首先检查是否有足够的空间 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.