View Single Post
Go4Expert Member
29Feb2012,20:10  
Chong's Avatar
Hi M
I have written a simple or sample program for you as below. I hope it helps you.
Best regards
Chong
++++++++++++++++++++++++++++
Code:
#include <stdio.h>
#include <string.h>

struct dummy {
	char address[100];
};

struct test {
	char name[20];
	int age;
	struct dummy home;
};

int main()
{
	struct test student[2];

	strcpy(student[0].name, "Chong Kim");
	student[0].age = 20;
	strcpy(student[0].home.address,"Funcy road");


	strcpy(student[1].name,"Christina Kim");
	student[1].age = 30;
	strcpy(student[1].home.address,"Tadcaster road");

	FILE *fw,*fr;

	if ((fw=fopen("test.txt","rw"))==NULL){
		printf("File open for reading or writing failed\n");
		return -1;
	}

	fwrite(student,sizeof(struct test),2,fw);

	fseek(fw,0,SEEK_SET);
	fread(student,sizeof(struct test),2,fw);

	printf("%s is %d old\n",student[0].name,student[0].age);
	printf("address:%s\n",student[0].home.address);
	printf("%s is %d old\n",student[1].name,student[1].age);
	printf("address:%s\n",student[1].home.address);


	fclose(fw);
}

Last edited by shabbir; 29Feb2012 at 20:28.. Reason: Code blocks