【问题标题】:C - How to store 2nd column of file in an array?C - 如何将第二列文件存储在数组中?
【发布时间】: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 编程的新手,所以请给我一个学习的机会!

编辑:我已经取得了一些进展(参见上面的编辑代码),现在我的变量pchcountif 语句中显示了正确的值。但是countriesArray 仍然没有正确填充

【问题讨论】:

  • 您正在声明一个 char* 指针数组,但您仍然需要为每个字符串分配存储空间。
  • countriesArray[count]=pch; --> countriesArray[count]=strdup(pch);
  • 谢谢!成功了。

标签: c arrays char token delimiter


【解决方案1】:

你重复使用getline,它为line分配一个缓冲区,最后只释放一行。对于任何严重的程序,它都会导致所谓的内存泄漏:您分配一块内存并松开可以释放它的指针。您应该有一个 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];
}

(仅此一点;不要尝试释放 countryArray 元素,因为它们只是分配元素内的指针,分配的元素在 lines

但至少,您所有的countriesArray 元素都指向不同的内存位置。

此代码中的真正问题是您在第一个循环之外将index 设置为0,例如count。这对 count 是正确的,但在第一个循环的每次迭代中,索引必须重置为 0。

【讨论】:

  • 感谢您的回答!但我已经尝试了你的建议,现在它进入了一个无限循环......它只是不断迭代第二个 while 循环
  • 我编辑了我的帖子以在第二列之后停止迭代。
  • 我可以运行你的程序...在我将free(countriesArray[i]) 替换为free(lines[i]) 并删除if ...{ free(line); } 之后。而且...我的第二个循环建议很愚蠢strok(lines, ",")strok(NULL, ",") 不同...
【解决方案2】:

感谢@BLUEPIXY 提供的建议,设法让它工作。原来我错过的只是strdup

代码:

FILE * fp;
char * line = NULL;
size_t len = 0;

int count=0;
ssize_t read;


fp = fopen("Countries.txt", "r");
if (fp == NULL)
    exit(EXIT_FAILURE);

       while ((read = getline(&line, &len, fp)) != -1) 
       {

           int index=0;
           char * pch;
           pch = strtok (line,",");
           int i;

           for (i=0; i<2; i++)
           {



              if (index == 1)
              { 
                    countriesArray[count]=strdup(pch);
              }


              pch = strtok (NULL, ",");
              index++;




           }

       count++;


       }





 if (line)
    free(line);

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2016-12-28
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多