【发布时间】:2014-08-06 14:11:20
【问题描述】:
我有一个文件,它以以下格式将数据存储在逗号分隔文件 (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 编程的新手,所以请给我一个学习的机会!
编辑:我已经取得了一些进展(参见上面的编辑代码),现在我的变量pch 和count 在if 语句中显示了正确的值。但是countriesArray 仍然没有正确填充
【问题讨论】:
-
您正在声明一个 char* 指针数组,但您仍然需要为每个字符串分配存储空间。
-
countriesArray[count]=pch;-->countriesArray[count]=strdup(pch); -
谢谢!成功了。
标签: c arrays char token delimiter