我正在從預讀取文件中讀取數據並將其存儲在緩衝區中,在該緩衝區中,我已經通過一個結構來組織數據,然後將其重新保存到另一個文件中。讀取文件中的多行數據
但是我只讀一行代碼。
我的代碼 - 打開文件:
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來獲取結構的大小,但我不知道如何去做這件事。任何幫助都感激不盡。
這一行:同時(與fgets(淺黃色,1000,p_file)= NULL缺少尾隨! ')' – user3629249 2014-12-05 11:02:54
這一行:如果((p_file = FOPEN(fileLocation, 「W」))== NULL缺少一個尾隨的')',對文件指針使用不同的名稱,其他明智的1)將無法進一步引用第一個文件,2)將無法關閉第一個文件3)只需要打開輸出文件一次,所以放置在廁所外面; – user3629249 2014-12-05 11:03:40