二进制文件读写
Binary files read & write
所以我的二进制文件有问题。(是的,我是初学者,但我知道一些事情。)
为什么我的二进制文件不是 "remember" 我上次 运行 我的程序写的?
这就像它只是删除所有内容或者只是无法读取文件中的内容,所以每次我 运行 我的程序都需要重新编写所有参数。
Header.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)->name
与 user[i].name
相同。
所以我的二进制文件有问题。(是的,我是初学者,但我知道一些事情。)
为什么我的二进制文件不是 "remember" 我上次 运行 我的程序写的?
这就像它只是删除所有内容或者只是无法读取文件中的内容,所以每次我 运行 我的程序都需要重新编写所有参数。
Header.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)->name
与 user[i].name
相同。