I am trying to write structure to a file and then read structure to a dynamicly allocated memory. If this algorhythm even works, I'm not sure about fread function... Is it able to read all structures in file written this way and store it to *ptr, so I can access it from memory ? Is there better way to do this?
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;
}
Firstly, There are lot of simple typo mistakes like
entrys = size / sizeof(num);
should be
entrys = size / sizeof(struct num);
ptr = (num*)malloc(entrys * sizeof(num));
should be ptr = malloc(entrys * sizeof(struct num));
. Avoid casting result of malloc()
.Secondly, after finding the size
, you forgot to move f
to beginning of file. for e.g
fseek(f, 0L, SEEK_END);
size = ftell(f);
fseek(f, 0L, SEEK_SET); /* you forgot to add this */
Thirdly, you are data into file using fwrite()
use ab
instead of a
mode
& while reading using fread()
use rb
instead of r
.
Also after reading from file you need to free the ptr
to avoid memory leakage.
free(ptr);
Here is the sample code
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;
}