我有一个文件将数据存储在逗号分隔文件 (Countries.txt) 中,格式如下(这只是一个小样本):
AD,Andorra,AN,AD,AND,20.00,Andorra la Vella,Europe,Euro,EUR,67627.00
AE,United Arab Emirates,AE,AE,ARE,784.00,Abu Dhabi,Middle East,UAE Dirham,AED,2407460.00
AF,Afghanistan,AF,AF,AFG,4.00,Kabul,Asia,Afghani,AFA,26813057.00
AG,Antigua and Barbuda,AC,AG,ATG,28.00,Saint John's,Central America and the Caribbean,East Caribbean Dollar,XCD,66970.00
AI,Anguilla,AV,AI,AIA,660.00,The Valley,Central America and the Caribbean,East Caribbean Dollar,XCD,12132.00
我想存储每一行的第二个字段,这样我的数组只包含国家名称,如下所示:
char *countriesArray[4096];
countriesArray[0] = "Andorra"
countriesArray[1] = "United Arab Emirates"
countriesArray[2] = "Afghanistan"
countriesArray[3] = "Antigua and Barbuda"
countriesArray[4] = "Anguilla"
但每次我运行我的代码时,我的数组都没有正确填充。我很确定问题不在于标记算法,因为一旦我删除了 if
语句,我就可以正确显示每个标记。这是我的代码:
FILE * fp;
char * line = NULL;
size_t len = 0;
int count=0;
ssize_t read;
char *countriesArray[4096];
fp = fopen("Countries.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
printf("First while loop iterating");
printf("%s", line);
int index=0;
char * pch;
pch = strtok (line,",");
int i;
for (i=0; i<2; i++)
{
printf("Second while loop iterating");
printf ("\npch is :%s\n",pch);
if (index == 1)
{
printf ("\nGoing to assign this to countriesArray:%s\n",pch);
printf ("\nVariable count is:%d\n",count);
countriesArray[count]=pch;
}
pch = strtok (NULL, ",");
index++;
}
count++;
}
printf("countriesArray at index 0 is :%s\n", countriesArray[0]);
printf("countriesArray at index 1 is :%s\n", countriesArray[1]);
printf("countriesArray at index 2 is :%s\n", countriesArray[2]);
printf("countriesArray at index 3 is :%s\n", countriesArray[3]);
int i;
for (i=0; i<count; i++)
{
free (countriesArray[i]);
}
if (line)
free(line);
exit(EXIT_SUCCESS);
输出:
First while loop iteratingAD,Andorra,AN,AD,AND,20.00,Andorra la Vella,Europe,Euro,EUR,67627.00
Second while loop iterating
pch is :AD
Second while loop iterating
pch is :Andorra
Going to assign this to countriesArray:Andorra
Variable count is:0
First while loop iteratingAE,United Arab Emirates,AE,AE,ARE,784.00,Abu Dhabi,Middle East,UAE Dirham,AED,2407460.00
Second while loop iterating
pch is :AE
Second while loop iterating
pch is :United Arab Emirates
Going to assign this to countriesArray:United Arab Emirates
Variable count is:1
First while loop iteratingAF,Afghanistan,AF,AF,AFG,4.00,Kabul,Asia,Afghani,AFA,26813057.00
Second while loop iterating
pch is :AF
Second while loop iterating
pch is :Afghanistan
Going to assign this to countriesArray:Afghanistan
Variable count is:2
First while loop iteratingAG,Antigua and Barbuda,AC,AG,ATG,28.00,Saint John's,Central America and the Caribbean,East Caribbean Dollar,XCD,66970.00
Second while loop iterating
pch is :AG
Second while loop iterating
pch is :Antigua and Barbuda
Going to assign this to countriesArray:Antigua and Barbuda
Variable count is:3
First while loop iteratingAI,Anguilla,AV,AI,AIA,660.00,The Valley,Central America and the Caribbean,East Caribbean Dollar,XCD,12132.00
Second while loop iterating
pch is :AI
Second while loop iterating
pch is :Anguilla
Going to assign this to countriesArray:Anguilla
Variable count is:4
countriesArray at index 0 is :Anguilla
countriesArray at index 1 is :Anguilla
countriesArray at index 2 is :Anguilla
countriesArray at index 3 is :Anguilla
*** Error in `./chef': free(): invalid pointer: 0x09c1c173 ***
Aborted (core dumped)
我是 C 编程的新手,所以请给我学习的机会!
EDIT :我取得了一些进展(见上面编辑过的代码),现在我的变量 pch
和 count
在内部显示了正确的值if
语句。但是 countriesArray
仍然没有正确填充
最佳答案
您重复使用 getline
为 line
分配缓冲区,最后只释放一行。对于任何严重的程序,它都会导致所谓的内存泄漏:您分配了一 block 内存并丢失了允许释放它的指针。您应该有一个 char *lines[4096]
数组来保留所有这些行,以便能够在最后正确释放它们。
char *lines[4096];
while ((read = getline(&line, &len, fp)) != -1)
{
lines[count] = line;
在程序结束时:
for(int i=0; i<count; i++) {
free(lines[i];
}
(仅此而已;不要尝试释放 countriesArray 元素,因为它们只是分配元素内的指针,分配的元素在行
中)
但至少,您所有的 countriesArray
元素都指向不同的内存位置。
此代码中的真正问题是您在第一个循环之外将 index
设置为 0,例如 count
。这对于 count
是正确的,但在第一个循环的每次迭代中必须将索引重置为 0。
关于C - 如何将文件的第二列存储在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25162636/