从键盘输入取并插入一个二进制文件 [英] Take input from keyboard and insert in a binary file
问题描述
我试图写一个函数来获得输入从用户的结构
的形式,将其插入一个二进制文件。 (足球结果)用户输入 teamA
, teamB
, goalsA
和 goalsB
当我尝试以显示我已经进入,我没有得到正确的结果。我究竟做错了什么?
code:
无效addResult()
{
结构匹配匹配;
字符输入[100];
FILE *文件1; 文件1 = FOPEN(matches.bin,RW + B);
的printf(请输入要添加到文件\\ n个resulst);
scanf函数(%S,输入); 而(!的feof(文件1))
FREAD(安培;匹配的sizeof(匹配),1,文件1);
fseek的(文件1,sizeof的(比赛),SEEK_END);
FWRITE(安培;输入的sizeof(匹配),1,文件1);
fflush(文件1);
FCLOSE(文件1); 文件1 = FOPEN(matches.bin,R);
而(!的feof(文件1)){
FREAD(安培;匹配的sizeof(匹配),1,文件1);
的printf(%s%S%I%I \\ N,match.teamA,match.teamB,match.goalsA,match.goalsB);
}
FCLOSE(文件1);
}
你的方法的主要问题是,由用户输入的字符串必须转换为结构的匹配
在某些时候。这样做是在C的烦人尴尬的任务:您需要先拆分用户输入空格分隔的单词或类似的,得到的字符串重新presentations为 .teamA
, .teamB
, .goalsA
和 .goalsB
(的例如的使用功能首页
从< strings.h>
),然后你需要将目标数字的字符串重新presentations使用的atoi
函数从&LT转换成整数值; stdlib.h中>
。
一旦你有了新的数据作为结构的匹配
,可以使用如下命令追加新的结构到文件中:
文件1 =的fopen(matches.bin,A + B);
FWRITE(安培; inputAsStruct,sizeof的(比赛),1,文件1);
文件模式 A +
打开用于读取和写入文件,创建如果需要一个新的文件,并确保写入是在末尾。
要输出不需要关闭并重新打开该文件中的记录。你可以只用 fseek的
将文件移动位置回到文件的开头:
fseek的(文件1,0,SEEK_SET);
最后,正如其他人所指出的,的feof
可能无法检测到文件末尾可靠。我相信它只能保证后,因为它已经达到文件末尾的 FREAD
失败了一次,所以你应该检查 FREAD <返回值工作/ code>代替。
I am trying to write a function to take input from user in the form of a struct
and insert it into a binary file. (of football results) the user is to enter teamA
, teamB
, goalsA
and goalsB
When I try to display what I have entered, I am not getting the correct result. What am I doing wrong?
Code:
void addResult()
{
struct matches match;
char input[100];
FILE * file1;
file1 = fopen("matches.bin","rw+b");
printf("Enter the resulst you want to add to the file\n");
scanf("%s",input);
while(!feof(file1))
fread(&match,sizeof(match),1,file1);
fseek(file1,sizeof(match),SEEK_END);
fwrite(&input,sizeof(match),1,file1);
fflush(file1);
fclose(file1);
file1 = fopen("matches.bin","r");
while(!feof(file1)) {
fread(&match,sizeof(match),1,file1);
printf("%s %s %i %i\n",match.teamA,match.teamB,match.goalsA,match.goalsB);
}
fclose(file1);
}
The main problem with your approach is, that the string inputted by the user must be converted to a struct matches
at some point. Doing so is an annoyingly awkward task in C: you will need to first split the user input into space-separated words or similar, to get string representations for .teamA
, .teamB
, .goalsA
and .goalsB
(e.g. using the function index
from <strings.h>
), and then you need to convert the string representations of the goal numbers into integer values using the atoi
function from <stdlib.h>
.
Once you have the new data as a struct matches
, you can append the new struct to the file using commands like the following:
file1 = fopen("matches.bin", "a+b");
fwrite(&inputAsStruct, sizeof(match), 1, file1);
The file mode a+
opens the file for reading and writing, creates a new file if needed, and ensures that writes are appended at the end.
To output the records you don't need to close and re-open the file. You can just use fseek
to move the file position back to the start of the file:
fseek(file1, 0, SEEK_SET);
Finally, as others pointed out, feof
may not detect the end of file reliably. I believe it is only guaranteed to work after the fread
failed once because it reached the end of file, so you should check the return value of fread
instead.
这篇关于从键盘输入取并插入一个二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!