我正在尝试将文本文件“data”的内容复制到名为“raf”的文本文件中。以下是“数据”文件的内容:
30 Clinton,Bill 300.30
50 Carter,Jimmy 500.50
60 Bush,George 60.60
20 Reagan,Ronald 200.20
10 Nixon,Kathy 100.10
40 Bush,Barba 400.40
90 Obama,Barack 900.90
我的目标是获得一个看起来像这样的输出:
ID NAME BALANCE
---------------------------------------
10 Nixon,Kathy 100.10
20 Reagan,Ronald 200.20
30 Clinton,Bill 300.30
40 Bush,Barba 400.40
50 Carter,Jimmy 500.50
60 Bush,George 60.60
0 bbbb 0.00
0 bbbb 0.00
90 Obama,Barack 900.90
0 bbbb 0.00
然而,这就是我实际获得的:
ID NAME BALANCE
---------------------------------------
10 Nixon,Kathy 100.10
20 Reagan,Ronald 200.20
30 Clinton,Bill 300.30
40 Bush,Barba 400.40
50 Carter,Jimmy 500.50
60 Bush,George 60.60
60 Bush,George 60.60
60 Bush,George 60.60
60 Bush,George 60.60
60 Bush,George 60.60
Press any key to continue . . .
这是我目前的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct PERSON
{
int ID;
char name[20];
float balance;
};
void DisplayText(char FullText[]);
void RAF(char *fname);
void DisplayRAF(char *fname);
void CopyText2RAF(char* txtname, char* rafname);
int main()
{
char FullText[500];
printf("Display text file\n\n");
DisplayText(FullText);
RAF("raf.txt");
//printf("\n\n");
//printf("Display after Initializing Randon Access File\n\n");
//DisplayRAF("raf.txt");
printf("\n\nDisplay after Copying text to Random Access File");
CopyText2RAF("data.txt","raf.txt");
printf("\n\n");
DisplayRAF("raf.txt");
printf("\n\n");
system("PAUSE");
return 0;
}
void DisplayText(char FullText[])
{
FILE * fp;
fp = fopen("data.txt","r");
int i;
printf("ID \t NAME \t\t BALANCE \n");
printf("---------------------------------------\n");
for(i=0;i<7; i++)
{
fgets(FullText,500,fp);
printf("%s", FullText);
}
printf("\n");
free(FullText);
fclose(fp);
}
void RAF(char *fname)
{
FILE * f;
struct PERSON p = {0 , "bbbb", 0.0};
// OPEN THE RAF FILE
f = fopen(fname,"wb");
//INITIALIZE THE FILE WITH RECORD p
int i;
for(i=1; i<=10; i++)
{
fwrite((char *)&p,sizeof(p),1,f);
}
fclose(f);
}
void DisplayRAF(char *fname)
{
FILE * f = fopen(fname,"rb");
struct PERSON p;
int i;
printf("ID \t NAME \t\t BALANCE \n");
printf("---------------------------------------\n");
for(i=1; i<=10; i++)
{
fread((char *)&p, sizeof(p),1,f);
printf("%d \t %s \t %.2f \n",p.ID, p.name, p.balance);
}
fclose(f);
}
void CopyText2RAF(char* txtname, char* rafname)
{
FILE * ftxt = fopen(txtname,"r");
FILE * fraf = fopen(rafname,"wb");
//READ FROM TEXT FILE
struct PERSON p;
fscanf(ftxt, "%d %s %f", &p.ID, p.name, &p.balance);
while(! feof(ftxt))
{
int ByteOfText = ((p.ID/10-1)*sizeof(p));
fseek(fraf,ByteOfText, SEEK_SET);
fwrite((char *)&p, sizeof(p), 1, fraf);
fscanf(ftxt, "%d %s %f", &p.ID, p.name, &p.balance);
}
fclose(ftxt);
fclose(fraf);
}
由于某些原因,我将文本文件复制到RAF文件后,显示器只读取到ID 60,然后重复直到for循环完成。我不明白为什么P被初始化的内容不会被打印出来用于ID 70和80. ID 90根本不打印。取而代之的是ID 60的信息。非常感谢任何帮助或建议。
答案 0 :(得分:1)
这里的问题是当您打开文件以在CopyText2RAF
函数中更新它时使用:
FILE * fraf = fopen(rafname,"wb");
这会打开一个空文件进行编写并销毁任何以前存在的内容。这就是为什么你没有任何以前写过的数据。
相反,您想要使用
FILE * fraf = fopen(rafname,"r+b");
r+
表示您希望文件打开以进行读取和写入(并且该文件必须已存在)。
对您的程序进行此更改并运行它我得到了结果:
将文本复制到随机访问文件后显示
ID NAME BALANCE
---------------------------------------
10 Nixon,Kathy 100.10
20 Reagan,Ronald 200.20
30 Clinton,Bill 300.30
40 Bush,Barba 400.40
50 Carter,Jimmy 500.50
60 Bush,George 60.60
0 bbbb 0.00
0 bbbb 0.00
0 bbbb 0.00
0 bbbb 0.00
但是你会注意到最后一个丢失了。这是因为你在循环结束时从文本文件中读取了值,然后循环回来并在继续之前检查它是否在文件的末尾。所以它永远不会写最后一个。 我将该功能修改为以下内容:
void CopyText2RAF(char* txtname, char* rafname)
{
FILE * ftxt = fopen(txtname,"r");
FILE * fraf = fopen(rafname,"r+b");
//READ FROM TEXT FILE
struct PERSON p;
while(! feof(ftxt))
{
int ByteOfText;
fscanf(ftxt, "%d %s %f", &p.ID, p.name, &p.balance);
ByteOfText = ((p.ID/10-1)*sizeof(p));
fseek(fraf,ByteOfText, SEEK_SET);
fwrite((char *)&p, sizeof(p), 1, fraf);
}
fclose(ftxt);
fclose(fraf);
}
现在它产生结果:
将文本复制到随机访问文件后显示
ID NAME BALANCE
---------------------------------------
10 Nixon,Kathy 100.10
20 Reagan,Ronald 200.20
30 Clinton,Bill 300.30
40 Bush,Barba 400.40
50 Carter,Jimmy 500.50
60 Bush,George 60.60
0 bbbb 0.00
0 bbbb 0.00
90 Obama,Barack 900.90
0 bbbb 0.00