簡體   English   中英

fgets()直到while循環中的文件末尾才繼續

[英]fgets() doesn't continue until to the end of the file in the while loop

我需要讀取一個文件直到結束,但是它在結束之前會中斷。 這是我的檔案;

8378749330196451143 7761927347639996843
3597172149842452783 2722311582032048570
6413963814675220040 151985128848019557
2797849130305409931 7006855242872793130
2312483514986641752 4723009976223322840
6299327914512777856 8560986736619134288
7840374864908954188 6413325942318176938
7120263339634576853 5721019237600873947
4064508404151769878 8332513917413808407
1131946326472490896 8768732125304950625
2593825059274639432 4898919688177170005
1152428805919273221 4955899780218674181
4762291655892416501 1286985264509566046
1098025338340897985 4952984927278789363
2524421559647997815 8843945302342585201

我正在使用這部分代碼來獲取代碼;

file = fopen ( buffer, "r" );
    if ( file != NULL ){
            while ( fgets ( line, sizeof line, file ) != NULL ) {
                // Token will point to the part before the =.
                pairs[i].key = strdup(strtok(line, search));
                // Token will point to the part after the =.
                pairs[i].value = strdup(strtok(NULL, search));

                i++;
              }
    }

它有效,但直到最后。 閱讀完以下部分后,while循環結束。 這可能是什么原因? 每行以\\ n符號結尾。

8378749330196451143 7761927347639996843
3597172149842452783 2722311582032048570
6413963814675220040 151985128848019557
2797849130305409931 7006855242872793130
2312483514986641752 4723009976223322840
6299327914512777856 8560986736619134288
7840374864908954188 6413325942318176938
7120263339634576853 572101923

注意:我從編輯器中截取了顯示標記的屏幕截圖

在此處輸入圖片說明

編輯2

您在此主題上的上一個問題表明,您一次處理文件1000行中的數據,對於這1000行中的每行,您調用strdup兩次(對於文本行中的每個數據字段一次)。 您還說過“ 我有一個包含2 ^ 32個鍵/值對的文件 ”。

對於其中的每一個,您都調用strdup兩次,每個數據字段一次。 現在, strdup分配內存來保存要復制的字符串。 有了大量的數據,就意味着要占用大量的內存,而程序由於缺乏數據而陷入停頓。

因此,我建議在處理完每1000行輸入后,釋放內存,如下所示-請適應您的需求,因為您從不顯示完整的代碼。

for (i=0; i<1000; i++) {
    free(pairs[i].key);
    free(pairs[i].value);
}

檢查返回錯誤提示值的任何庫函數的返回值本質上是一種好習慣。 strdup的情況strdup ,它將告訴您是否內存不足。 strtok的情況下,您會知道該文件中包含流氓行,您無法解釋-可能在文件末尾由空行終止的空newline

其次,在前一個問題中的注釋揭示了char line [c]

定義為41個字符,因為我的每一行最多可以為41個字符。

現在,一個64位unsigned int最多可以有20個十進制數字。 其中兩個加上一個space使之成對。41. 字符串終止符呢? 那么newline通常會出現在fgets獲得的輸入的末尾呢? 我建議您將c的定義(從不顯示)更改為

#define c 50

因為沒有理由用單個字符串長度來緊。 當您調用strdup ,只會為字符串長度+終止符分配足夠的內存,因此根本就不會浪費太多內存。

該程序可以從文件中正確讀取數據-

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct data{
    char *key;
    char *value;
}pairs[20];

int main(void) {
    FILE *fp; 
    int i=0;
    char line[255],search[2]=" ";
    char *token;
    fp=fopen("Results.txt","r");
    if(fp!=NULL){
       while(fgets(line,sizeof line,fp)!=NULL){            // read from file 
         pairs[i].key=NULL;                // setting to NULL 
         pairs[i].value=NULL;              // setting to NULL 
         token=strtok(line, search);
         if(token!=NULL){             // check its return 
              pairs[i].key = strdup(token);
           }
         token=strtok(NULL, search);
         if(token!=NULL){            // check its return             
             pairs[i].value = strdup(token);
         }            
         if(pairs[i].key!=NULL && pairs[i].value!=NULL)      //check return from strdup
              printf("%s %s\n",pairs[i].key,pairs[i].value);  //print values
           i++;
        }
    }
   for(int j=0;j<i;j++) {
      free( pairs[i].key);
      free( pairs[i].value);
   }
   fclose(fp);
}

我不知道你的聲明,因此也不知道。

暫無
暫無

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

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