簡體   English   中英

C-如何將文件的第二列存儲在數組中?

[英]C - How to store 2nd column of file in an array?

我有一個文件,該文件以以下格式(這只是一個小樣本)以逗號分隔文件(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仍然無法正確填充

重復使用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];
}

(不僅如此;不要試圖免費countriesArray元素大公只是指針內分配要素,分配的元素是lines

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

這段代碼中的真正問題是在第一個循環之外將index設置為0,例如count 這對於count是正確的,但是必須在第一個循環的每次迭代中將索引重置為0。

感謝@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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM