This is the include file test.h
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
struct _address
{
char *Street;
int HouseNumber;
} ;
struct _name
{
int age;
char *lastname, *firstname;
struct _address *addr;
} ;
struct _student
{
int num;
struct _name *NameOfStudent;
} ;
This is the write.c code to write data into binary file:
Code:
#include "test.h"
int main()
{
FILE *f;
struct _student *hs1, *hs;
hs1 = (struct _student *)malloc(sizeof(struct _student));
hs1->NameOfStudent = (struct _name*)malloc(sizeof(struct _name));
hs1->NameOfStudent->addr = (struct _address*)malloc(sizeof(struct _address));
hs1->NameOfStudent->lastname = "John";
hs1->NameOfStudent->firstname = "A";
hs1->NameOfStudent->age = 15;
hs1->NameOfStudent->addr->Street = " London ";
hs1->NameOfStudent->addr->HouseNumber = 123;
hs1->num = 1;
printf(" ------------- Input ----------------\n");
printf(" %d %s %s %d %s \n",
hs1->num, hs1->NameOfStudent->lastname,
hs1->NameOfStudent->firstname,
hs1->NameOfStudent->addr->HouseNumber,
hs1->NameOfStudent->addr->Street);
f = fopen ("/root/test","wb");
fwrite((struct _student *)hs1, sizeof(struct _student), 1, f);
fwrite((struct _name *)hs1->NameOfStudent, sizeof(struct _name), 1, f);
fwrite((struct _address *)hs1->NameOfStudent->addr, sizeof(struct _address), 1, f);
fclose(f);
free(hs1->NameOfStudent->addr);
free(hs1->NameOfStudent);
free(hs1);
return 0;
}
This is the read.c code to read from binary file
Code:
#include "test.h"
struct _student STUDENT;
struct _address ADDRESS;
struct _name NAME;
int main()
{
FILE *f;
struct _student *hsRead;
struct _name *name;
struct _address *addr;
hsRead = &STUDENT;
name = &NAME;
addr = &ADDRESS;
printf(" sizeof(struct _student) = %d \n", sizeof(struct _student));
printf(" sizeof(struct _name) = %d \n", sizeof(struct _name));
printf(" sizeof(struct _address) = %d \n", sizeof(struct _address));
printf(" ------------- Readfile ----------------\n");
//hs = (struct _student *)malloc(sizeof(struct _student));
f = fopen ("/root/test","rb");
while(!feof(f))
{
int status = fread((struct _student *)hsRead, sizeof(struct _student)+sizeof(struct _name)+sizeof(struct _address), 1, f);
if (status > 0)
{
name = hsRead->NameOfStudent;
addr = hsRead->NameOfStudent->addr;
printf(" STT : %d \n", hsRead->num);
printf(" NAME : %s %s \n", name->lastname, name->firstname);
printf(" AGE(s) : %d \n", name->age);
printf(" ADDR : %d %s \n", addr->HouseNumber, addr->Street);
}
}
fclose(f);
return 0;
}
The result of read.c is
Code:
sizeof(struct _student) = 8
sizeof(struct _name) = 16
sizeof(struct _address) = 8
------------- Readfile ----------------
STT : 1
NAME : (null) (null)
AGE(s) : 0
Segmentation fault (core dumped)
Why the result of NAME show here is (null) (null) and AGE(s) is 0 and the Segmentation fault ??? When I write the code WRITE and READ in ONE C file, it's ok. How can i fix it ??? Please help me. I'm a newbie in C on Linux.
Thanks.