void save_records() { FILE *fp; fp = fopen("records.dat", "wb"); if (fp == NULL) { printf("Failed to open file!\n"); return; } fwrite(records, sizeof(struct Employee), num_records, fp); fclose(fp); printf("Records have been saved to file successfully!\n"); } 解决一下这段代码保存后打开查看是乱码的问题
时间: 2024-01-17 07:04:03 浏览: 83
这个问题可能是因为写入文件时使用了二进制模式,但是读取文件时却没有使用二进制模式。建议在读取文件时也使用二进制模式打开文件,例如:
```c++
void load_records() {
FILE *fp;
fp = fopen("records.dat", "rb");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
fread(records, sizeof(struct Employee), num_records, fp);
fclose(fp);
printf("Records have been loaded from file successfully!\n");
}
```
另外,如果你在写入文件时使用了结构体,建议在读取文件时也使用相同的结构体来读取。这样可以保证读取的数据与写入的数据类型一致。
相关问题
void save_records() { FILE *fp; fp = fopen("records.dat", "rb"); if (fp == NULL) { printf("Failed to open file!\n"); return; } fwrite(records, sizeof(struct Employee), num_records, fp); fclose(fp); printf("Records have been saved to file successfully!\n"); } void load_records() { FILE *fp; fp = fopen("records.dat", "rb"); if (fp == NULL) { printf("Failed to open file!\n"); return; } 解决c语言项目保存打开后乱码的问题
在函数save_records()中,文件打开模式应该为"wb" (写入二进制),而不是"rb" (读取二进制)。修改代码如下:
```c
void save_records() {
FILE *fp;
fp = fopen("records.dat", "wb");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
fwrite(records, sizeof(struct Employee), num_records, fp);
fclose(fp);
printf("Records have been saved to file successfully!\n");
}
```
在函数load_records()中,同样应该使用"rb"模式打开文件。另外,在读取完数据后,需要检查读取的数据数量是否与实际数量相符。如果不相符,说明文件中的数据可能有误。修改代码如下:
```c
void load_records() {
FILE *fp;
fp = fopen("records.dat", "rb");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
// 读取数据到sorted_records数组中
int num_read = fread(sorted_records, sizeof(struct Employee), MAX_RECORDS, fp);
if (num_read != num_records) {
printf("Error: The number of records read from file is not consistent with the actual number of records!\n");
fclose(fp);
return;
}
fclose(fp);
printf("Records have been loaded from file successfully!\n");
}
```
另外,还需要注意一下读取和写入的顺序需要一致。如果读取时使用了"rb"模式,那么保存时也应该使用"rb"模式。同理,如果读取时使用了"r"模式,那么保存时也应该使用"w"模式。
使用全局顺序表结构解决void save_records() { FILE *fp; fp = fopen("records.dat", "wb"); if (fp == NULL) { printf("Failed to open file!\n"); return; } fwrite(records, sizeof(struct Employee), num_records, fp); fclose(fp); printf("Records have been saved to file successfully!\n"); } 这段代码保存后打开查看是乱码的问题
如果要使用全局顺序表结构来解决这个问题,可以将结构体数组 `records` 定义为全局变量,并在保存和读取数据时使用该全局变量。具体代码如下:
```c++
#include <stdio.h>
#include <stdlib.h>
#define MAX_RECORDS 100
struct Employee {
int id;
char name[20];
int age;
};
struct Employee records[MAX_RECORDS];
int num_records = 0;
void save_records() {
FILE *fp;
fp = fopen("records.dat", "wb");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
fwrite(records, sizeof(struct Employee), num_records, fp);
fclose(fp);
printf("Records have been saved to file successfully!\n");
}
void load_records() {
FILE *fp;
fp = fopen("records.dat", "rb");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
num_records = fread(records, sizeof(struct Employee), MAX_RECORDS, fp);
fclose(fp);
printf("Records have been loaded from file successfully!\n");
}
int main() {
// 其他代码
return 0;
}
```
在这个代码中,我们将结构体数组 `records` 和记录数 `num_records` 定义为全局变量,可以在 `save_records` 和 `load_records` 函数中直接使用这些全局变量来保存和读取数据。
阅读全文