【发布时间】:2016-09-16 19:52:33
【问题描述】:
我正在用 C 语言编写一个程序,它从用户那里读取文件数据(名称、q1、q2、q3 的答案),并将数据存储在 .txt 文件中,并允许用户查看他们输入的数据.目前,我对允许数据查看最后一条记录的功能有困难。 这是我的代码:
struct pre_survey{
char name[20];
int q1;
int q2;
int q3;
};
struct pre_survey temp;
struct pre_survey get;
int main(){
while (pre_or_post!=0){
if(pre_or_post==1){
printf("\n1. append(enter) data\n");
printf("2. check first record\n");
printf("3. check last record\n");
printf("please select your choice\n");
scanf("%d", &choice);
switch (choice){
case 1: append();
break;
case 2: frst_record();
break;
case 3: last_record();
break;}}
void append(){
fp=fopen("pre-survey.txt", "a");
printf("name\n");
scanf("%s", &temp.name);
printf("q1:\n");
scanf("%d", &temp.q1);
printf("q2:\n");
scanf("%d", &temp.q2);
printf("q3:\n");
scanf("%d", &temp. q3);
fprintf(fp, "%s %d %d %d", temp.name, temp.q1, temp.q2, temp.q3);
fclose(fp);}
void last_record(){
fp=fopen("pre-survey.txt", "r");
fseek(fp, -sizeof(temp),SEEK_END);
fscanf(fp, "%s %d %d %d", get.name, &get.q1, &get.q2, &get.q3);
printf("name: %s\n", get.name);
printf("q1: %d\n", get.q1);
printf("q2: %d\n", get.q2);
printf("q3:%d\n", get.q3);
fclose(fp);
}
现在,当我尝试查找最后一条记录时,会显示第一条记录的数据。我认为问题在于,当我检查 sizeof(temp) 为 32 时,以及当我使用
fp=fopen("pre-survey.txt", "r");
fseek(fp,0, 2);
size=ftell(fp);
printf("size:%d\n", size);
要检查整个文件的大小,它是 34。 因此,当我按 temp 的大小从文件末尾读取时,它会转到第一条记录。 但我不确定我在代码中做错了什么。
【问题讨论】:
-
当
main的第一行是while (pre_or_post!=0)但pre_or_post没有定义,甚至没有修改以影响循环行为时,这不是一个很好的开始。请提供Minimal, Complete, and Verifiable example,它显示了您尝试过的内容。 -
@bonny 你需要写固定大小。
-
随机访问和文本文件不能很好地结合在一起。
-
发布的代码无法编译。 main() 函数末尾似乎至少缺少 2 行代码。
-
为了便于阅读和理解: 1) 确保发布的代码在发布之前能够干净地编译。 2) 遵循公理:每行只有一个语句,并且(最多)每条语句有一个变量声明。 3) 始终缩进代码。在每个左大括号 '{' 后缩进。在每个右大括号 '}' 之前取消缩进。 4) 不要在任何其他代码语句之后在行尾隐藏右大括号'}'。