gpt4 book ai didi

c - 将结构写入文件并将结构读取到动态分配的内存

转载 作者:行者123 更新时间:2023-11-30 14:48:07 24 4
gpt4 key购买 nike

我正在尝试将结构写入文件,然后将结构读取到动态分配的内存中。如果这个算法甚至有效,我不确定 fread 函数...它是否能够读取以这种方式编写的文件中的所有结构并将其存储到 *ptr,以便我可以从内存中访问它?有更好的方法吗?

 typedef struct num {
int num1;
int num2;
}NR;

void write() {
int successfullywritten;
FILE *f;
struct num nr;
f = fopen("dat.txt", "a");
printf("Enter first number\n");
scanf("%d", &nr.num1);
printf("Enter second number\n");
scanf("%d", &nr.num2);
successfullywritten=fwrite(&nr, sizeof num, 1, f);
printf("Succesfully written: %d\n", successfullywritten);
fclose(f);
}

void read() {

int size, entrys,i,successfullyread;
FILE *f;
f = fopen("dat.txt", "r");
struct num *ptr;
fseek(f, 0L, SEEK_END);
size = ftell(f);
printf("\nSize of file dat.txt: %d\n", size);
entrys = size / sizeof(num);
ptr = (num*)malloc(entrys * sizeof(num));
successfullyread=fread(ptr, sizeof(num), entrys, f);
printf("sucessfully Read: %d\n", successfullyread);
for (i = 0;i < entrys; i++) {
/*fread((ptr+i), sizeof(ptr), 1, f);*/
printf("%d ", (*(ptr)).num1);
printf("%d\n", (*(ptr)).num2);

}
printf("\n");printf("\n");

fclose(f);
}

int main() {
int n;
while(1){
printf("Chooose action:\n1) write to file\n2) read from file:\n");
scanf("%d", &n);
switch (n) {
case 1:
write();
break;
case 2:
read();
break;
}
}
return 0;
}

最佳答案

首先,有很多简单的拼写错误,例如

  • 类型定义名称或结构名称之间存在混淆。这entrys = size/sizeof(num); 应该是entrys = size/sizeof(struct num);
  • ptr = (num*)malloc(entrys * sizeof(num)); 应为 ptr = malloc(entrys * sizeof(struct num)); 。避免转换malloc()的结果。

其次,找到大小后,您忘记将f移动到文件开头。例如

fseek(f, 0L, SEEK_END);
size = ftell(f);
fseek(f, 0L, SEEK_SET); /* you forgot to add this */

第三,使用fwrite()将数据写入文件,使用ab而不是a模式& 使用 fread() 读取时使用 rb 而不是 r

此外,从文件读取后,您需要释放ptr以避免内存泄漏。

free(ptr);

这是示例代码

typedef struct num {
int num1;
int num2;
}NR;
void my_write(void) {
int successfullywritten;
FILE *f;
struct num nr;
f = fopen("dat.txt", "ab");
printf("Enter first number\n");
scanf("%d", &nr.num1);
printf("Enter second number\n");
scanf("%d", &nr.num2);
successfullywritten=fwrite(&nr, sizeof(nr), 1, f);
printf("Succesfully written: %d\n", successfullywritten);
fclose(f);
}

void my_read(void) {

int size, entrys,i,successfullyread;
FILE *f;
f = fopen("dat.txt", "rb");
struct num *ptr;
fseek(f, 0L, SEEK_END);
size = ftell(f);
fseek(f, 0L, SEEK_SET);
printf("\nSize of file dat.txt: %d\n", size);
entrys = size / sizeof(struct num);

ptr = malloc(entrys * sizeof(struct num));
successfullyread=fread(ptr, sizeof(struct num), 2, f);
printf("sucessfully Read: %d\n", successfullyread);

for (i = 0;i < entrys; i++) {
/*fread((ptr+i), sizeof(ptr), 1, f);*/
printf("%d ", ptr[i].num1);
printf("%d\n", ptr[i].num2);

}
free(ptr);
printf("\n");printf("\n");
fclose(f);
}
int main(void) {
int n;
while(1){
printf("Chooose action:\n1) write to file\n2) read from file:\n");
scanf("%d", &n);
switch (n) {
case 1:
my_write();
break;
case 2:
my_read();
break;
}
}
return 0;
}

关于c - 将结构写入文件并将结构读取到动态分配的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50680068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com