gpt4 book ai didi

c - 二进制文件读写

转载 作者:行者123 更新时间:2023-11-30 16:32:19 25 4
gpt4 key购买 nike

所以我对二进制文件有疑问。(是的,我是初学者,但我知道一些事情。)

为什么我的二进制文件不“记住”我上次运行程序时写的内容?

这就像它只是删除所有内容或只是无法读取文件内的内容,因此每次运行程序时我都需要再次写入所有参数。

标题.h

#ifndef HEADER_H
#define HEADER_H

typedef struct user {
char name[30];
char last_name[30];
int id_number;
int phone;
} USER;

void reading(FILE*, int, USER*);
void writeing(FILE*, int, USER*);


#endif

这是在 main(Source.c) 内部

int main() {

int n = 0, user_number = 0;

USER *user = NULL;
user = (USER*)calloc(100, sizeof(USER));
FILE *dat = NULL;

while (n != 4) {
printf("Press 1 for creating binary file! \n");
printf("Press 2 for reading! \n");
printf("Press 3 to enter new user! \n");
printf("Press 4 to exit!\n\n");
printf("Number: ");
scanf("%d", &n);
printf("\n");

switch (n)
{
case 1:
dat = fopen("User.bin", "wb");
printf("File created.\n");
fclose(dat);
break;

case 2:
dat = fopen("User.bin", "rb");
reading(dat, user_number, user);
printf("\n");
fclose(dat);
break;

case 3:
user_number++;
printf("Insert details of %d user: \n", user_number);
dat = fopen("User.bin", "ab");
writeing(dat, user_number, user);
printf("\n");
fclose(dat);
break;

case 4:
printf("Program closed!\n");
break;

default:
break;
}
}
free(user);
system("PAUSE");
return 0;
}

这是内部函数,函数也组织在不同的文件(Functions.c)中。

void writeing(FILE* dat, int user_number, USER* user) {

int i = user_number- 1;

printf("Name: ");
scanf("%s", (user + i)->name);

printf("Last name: ");
scanf("%s", (user + i)->last_name);

printf("ID number: ");
scanf("%d", &(user + i)->id_number);

printf("Phone: ");
scanf("%d", &(user + i)->phone);
fwrite(user, sizeof(*user), 1, dat);
}


void reading(FILE* dat, int user_number, USER* user)
{
fread(user, sizeof(USER), user_number, dat);
for (int i = 0; i < user_number; i++)
{
fprintf(stdout, "Name: %s\nLast name: %s\nID number: %d\nPhone: 0%d\n", (user + i)->name, (user + i)->last_name, (user + i)->id_number, (user + i)->phone);
}
}

最佳答案

您没有向文件写入任何内容,至少没有在您提供的代码片段中写入任何内容。也许您想要做的是:

// my guess what USER is, because you didn't provide.....
typedef struct USER_s {
char name[200];
char last_name[200];
int id_number;
int phone;
} USER;

void writeing(FILE* dat, int user_number, USER* user) {

int i = user_number- 1;

printf("Name: ");
scanf("%s", (user + i)->name);

printf("Last name: ");
scanf("%s", (user + i)->last_name);

printf("ID number: ");
scanf("%d", &(user + i)->id_number);

printf("Phone: ");
scanf("%d", &(user + i)->phone);

// write 1 time sizeof(USER) length data pointed to by 'user' variable to dat file
size_t ret = fwrite(&user[i], sizeof(*user), 1, dat);
// assertion for safety
assert(ret == sizeof(*user));
}

void ispis(FILE* dat, int user_number, USER* user)
{
// read from dat file user_number times sizeof(USER) length data and save to user array
// user pointer can handle sizeof(USER)*user_number bytes
size_t ret = fread(user, sizeof(USER), user_number, dat);
// assertion for safety
assert(ret == sizeof(USER)*user_number);

for (int i = 0; i < user_number; i++)
{
fprintf(stdout, "Name: %s\nLast name: %s\nID number: %d\nPhone: 0%d\n", (user + i)->name, (user + i)->last_name, (user + i)->id_number, (user + i)->phone);
}
}

顺便说一句。您可能会感兴趣,(user + i)->nameuser[i].name 相同。

关于c - 二进制文件读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202374/

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