2014-12-05 145 views
0

我正在從預讀取文件中讀取數據並將其存儲在緩衝區中,在該緩衝區中,我已經通過一個結構來組織數據,然後將其重新保存到另一個文件中。讀取文件中的多行數據

但是我只讀一行代碼。

我的代碼 - 打開文件:

File *p_file 

char fileLocation[40]; 
char buff[1000]; 

printf("\nEnter file name: \n"); 
scanf("%s, fileLocation); 

p_file = fopen(fileLocation, "r"); 

if(!p_file) 
{ 
    printf("\nError!\n"); 
} 

循環讀取數據並保存文件

while(fgets(buff, 1000, p_file)!=NULL 
{ 
    printf("%s", buff); 

    storedData.source = atoi(strtok(buff, ":"); 
    storedData.destination = atoi(strtok(0, ":"); 
    storedData.type = atoi(strtok(0, ":"); 
    storedData.port = atoi(strtok(0, ":"); 
    storedData.data = strtok(0, ":\n"); 

    printf("\nEnter a File Name to save:"); 
    scanf("%s", fileLocation); 

if ((p_file = fopen(fileLocation, "w")) == NULL 
{ 
    puts("\n Could not point to the file."); 
}else{ 
    printf("\nSaving"); 
    fprintf(p_file, "%04d:%04d:%04d:%04:%s \n", 
      storedData.source, 
      storedData.destination, 
      storedData.type, 
      storedData.port, 
      storedData.data); 

    fclose(p_file); 
} 
fclose(p_file); 

數據的電流輸出:

0001:0002:0003:0021:CLS 

數據的通緝輸出:

0001:0002:0003:0021:CLS 
0001:0010:0003:0021:CLS 
0001:0002:0002:0080:<HTML> 

我相信我必須聲明一個整數值來循環遍歷文件內容以及使用malloc來獲取結構的大小,但我不知道如何去做這件事。任何幫助都感激不盡。

+0

這一行:同時(與fgets(淺黃色,1000,p_file)= NULL缺少尾隨! ')' – user3629249 2014-12-05 11:02:54

+0

這一行:如果((p_file = FOPEN(fileLocation, 「W」))== NULL缺少一個尾隨的')',對文件指針使用不同的名稱,其他明智的1)將無法進一步引用第一個文件,2)將無法關閉第一個文件3)只需要打開輸出文件一次,所以放置在廁所外面; – user3629249 2014-12-05 11:03:40

回答

1

你是過度使用p_file文件讀取以及文件寫入。

if ((p_file = fopen(fileLocation, "w")) == NULL) 

有了這個,你就失去了打開文件的指針來閱讀。 而當你在else部分關閉時,fgets()認爲沒有更多的線條。

使用其他一些變量來寫入文件。


如果你想工作緩衝數據,然後改變你的while(fgets()...讀取所有行,然後在每行工作。 fgets()不會讀取多行。

1

你的循環實際上做的事情只有一次

storedData.data = strtok(0, ":\n");

,所以你只取前行。

0
FILE *in_file; 
FILE *out_file; 
char fileLocation[40]; 
char buff[1000]; 

printf("\nEnter file name: \n"); 
if(1 != scanf(" %s, fileLocation)) 
{ 
    perror(" scanf failed for input file:); 
    exit(EXIT_FAILURE); 
} 

if(NULL == (in_file = fopen(fileLocation, "r")) 
{ 
    perror("fopen failed for input file"); 
    exit(EXIT_FAILURE); 
} 

printf("\nEnter a File Name to save:"); 
if(1 != scanf(" %s", fileLocation)) 
{ 
    perror("scanf failed for outut file name"); 
    fclose(in_file); // cleanup 
    exit(EXIT_FAILURE); 
} 

if (NULL == (out_file = fopen(fileLocation, "w"))) 
{ 
    perror(" fopen failed for output file"); 
    fclose(in_file); // cleanup 
    exit(EXIT_FAILURE)l 
} 

while(fgets(buff, 1000, p_file)!=NULL)) 
{ 
    printf("%s", buff); 

    storedData.source = atoi(strtok(buff, ":"); 
    storedData.destination = atoi(strtok(0, ":"); 
    storedData.type = atoi(strtok(0, ":"); 
    storedData.port = atoi(strtok(0, ":"); 
    storedData.data = strtok(0, ":\n"); 

    printf("\nSaving"); 
    fprintf(p_file, "%04d:%04d:%04d:%04:%s \n", 
     storedData.source, 
     storedData.destination, 
     storedData.type, 
     storedData.port, 
     storedData.data); 
} // end while 

fclose(in_file); // cleanup 
fclose(out_file); // cleanup