while (fread(&stu, LEN, 1, fp))
时间: 2023-09-28 10:14:15 浏览: 190
这是一个 C 语言中常见的文件读取操作,其中:
- `fread` 是一个标准库函数,用于从文件中读取数据;
- `&stu` 是一个指向结构体变量 `stu` 的指针,表示将读取的数据存储到该结构体中;
- `LEN` 是读取数据的长度,单位是字节;
- `1` 表示读取数据的数量,这里表示读取一条记录;
- `fp` 是一个指向文件的指针,表示要读取的文件。
整个语句的意思是从文件中读取一条长度为 `LEN` 字节的记录,并将其存储到结构体变量 `stu` 中。如果读取成功,函数返回值为 `1`,否则返回值为 `0`。
相关问题
fseek(fp, 0, SEEK_SET); while (fread(&stu, LEN, 1, fp)) { if (strcmp(x, stu.id) == 0) { printf("请重新输入图书id: "); scanf("%s", stu.id); printf("请重新输入书名: "); scanf("%s", stu.name); printf("请重新输入书籍作者 : "); scanf("%s", &stu.autho
r); printf("请重新输入价格: "); scanf("%f", &stu.price); fseek(fp, -LEN, SEEK_CUR); fwrite(&stu, LEN, 1, fp); printf("修改成功!\n"); break; } }
请问这段代码的功能是什么?
修改这段代码 void in() { int i, m = 0; char ch[2]; FILE* fp; if ((fp = fopen("d:/学生系统/student.txt", "ab+")) == NULL) { printf("can not open\n"); return; } while (!feof(fp)) { if (fread(&stu[m], LEN, 1, fp) == 1) m++; } fclose(fp); if (m == 0) printf("No record!\n"); else { system("cls"); show(); } if ((fp = fopen("d:/学生系统/student.txt", "wb")) == NULL) { printf("can not open\n"); return; } for (i = 0; i < m; i++) fwrite(&stu[i], LEN, 1, fp); printf("please input(y/n):"); scanf("%s", ch); while (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) { printf("学号:"); scanf("%d", &stu[m].num); for (i = 0; i < m; i++) if (stu[i].num == stu[m].num) { printf("学号已存在!"); _getch(); fclose(fp); return; } printf("姓名:"); scanf("%s", stu[m].name); printf("性别:"); scanf("%s", stu[m].sex); printf("出生年:"); scanf("%d", &stu[m].year); printf("出生月:"); scanf("%d", &stu[m].month); printf("出生日:"); scanf("%d", &stu[m].day); printf("c语言:"); scanf("%f", &stu[m].C); printf("高数:"); scanf("%f", &stu[m].math); printf("大学物理:"); scanf("%f", &stu[m].physics); stu[m].sum = stu[m].C + stu[m].math + stu[m].physics; stu[m].avg = stu[m].sum / 3; if (fwrite(&stu[m], LEN, 1, fp) != 1) { printf("can not save!"); _getch(); } else { printf("%s saved!\n", stu[m].name); m++; } printf("continue?(y/n):"); scanf("%s", ch); } fclose(fp); printf("OK!\n"); }
void in() {
int i, m = 0;
char ch[2];
FILE* fp;
if ((fp = fopen("d:/学生系统/student.txt", "ab+")) == NULL) {
printf("can not open\n");
return;
}
while (fread(&stu[m], LEN, 1, fp) == 1) {
m++;
}
fclose(fp);
if (m == 0) {
printf("No record!\n");
}
else {
system("cls");
show();
}
if ((fp = fopen("d:/学生系统/student.txt", "ab")) == NULL) {
printf("can not open\n");
return;
}
printf("please input(y/n):");
scanf("%s", ch);
while (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) {
printf("学号:");
scanf("%d", &stu[m].num);
for (i = 0; i < m; i++) {
if (stu[i].num == stu[m].num) {
printf("学号已存在!");
_getch();
fclose(fp);
return;
}
}
printf("姓名:");
scanf("%s", stu[m].name);
printf("性别:");
scanf("%s", stu[m].sex);
printf("出生年:");
scanf("%d", &stu[m].year);
printf("出生月:");
scanf("%d", &stu[m].month);
printf("出生日:");
scanf("%d", &stu[m].day);
printf("c语言:");
scanf("%f", &stu[m].C);
printf("高数:");
scanf("%f", &stu[m].math);
printf("大学物理:");
scanf("%f", &stu[m].physics);
stu[m].sum = stu[m].C + stu[m].math + stu[m].physics;
stu[m].avg = stu[m].sum / 3;
if (fwrite(&stu[m], LEN, 1, fp) != 1) {
printf("can not save!");
_getch();
}
else {
printf("%s saved!\n", stu[m].name);
m++;
}
printf("continue?(y/n):");
scanf("%s", ch);
}
fclose(fp);
printf("OK!\n");
}
修改说明:
1. 删除了 while (!feof(fp)) 循环,因为这种方式会导致最后一个数据重复读入。
2. 修改了打开文件的方式,将 "wb" 修改为 "ab",这样可以在文件末尾添加数据。
3. 将读取文件和写入文件的操作分开,避免因为文件指针位置不正确而导致的错误。
4. 修改了文件指针的关闭方式,在函数的各个分支中都关闭了文件指针,避免了文件指针未关闭的风险。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)