I am just learning to work with FILE
s. My problem is this:
when I want to read a binary file that was opened by fopen("example.dat", "ab")
, it shows something like:
But when I open file by fopen("example.dat", "wb")
, it works!
Here are my test programs:
to write on file:
#include <stdio.h>
void main() {
int i = 0;
FILE *save = fopen("save.dat", "wb");
char str[5][40];
while (i < 5) {
gets(str[i]);
fwrite(str[i], sizeof(char), 40, save);
i++;
}
}
to read:
#include <stdio.h>
void main() {
char str2[100][400];
int i = 0;
FILE *load = fopen("save.dat", "rb");
if (!load) {
printf("cant open file!");
return;
}
fread(str2[i], sizeof(char), 40, load);
while (!feof(load)) {
puts(str2[i]);
printf("\n");
i++;
fread(str2[i], sizeof(char), 40, load);
}
}
I need "ab"
because it is about saving info of a game and it shouldn't delete after closing program or run it again .