2014-12-05 73 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