So I have a problem with binary file.(Yea i'm a beginner, but I know some things.)
Why does my binary file not "remember" what I wrote last time I run my program?
It's like it just deletes everything or just can't read what's inside file, so every time I run my program I need to write all parameteres again.
Header.h
using (StreamReader sr = new StreamReader(File.OpenRead(s)))
{
textToReturn = sr.ReadToEnd();
}
This is inside main(Source.c)
Thread.Sleep(2500)
This is inside function, also functions are organized in different file(Functions.c).
#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
答案 0 :(得分:0)
您不会在文件中写入任何内容,至少不会在您提供的代码段中写入任何内容。您可能想要做的是:
// 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
相同。