nested struct , fread ,fwrite

This is a discussion on nested struct , fread ,fwrite within the C Programming forums, part of the General Programming Boards category; i haf my nested struct ready ... i'm trying to add a new thing into my data base that is ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    10

    nested struct , fread ,fwrite

    i haf my nested struct ready ... i'm trying to add a new thing into my data base that is books loan.. there fore i haf a main struct which shows the name and info all tat and nested struct in the main for the books.. well my problem is how do i write into the same file as the main struct ? and read from the file...

    is this correct
    eg.fwrite(&info.book.sizeof info.book,clfile); << For writing the nested struct into the file ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It helps if you try to form whole words and complete sentences. For example:
    "Hi, I have created a file, which has a given structure in it. I've modified the structure, and want to rewrite the original file, with the new structure. How can I do this?"
    Now, I have to assume that's what you meant. See how much clearer it was when I stated it? At any rate, here's what you do:
    Code:
    struct oldstruct
    {
        ...stuff...
    };
    struct newstruct
    {
        ...stuff + new stuff...
    };
    
    open file for reading
    open another for writing
    while not at the end of the input file
        read into an instance of old structure
        assign old data members across to the new structure instance
        fill the other "new stuff" portion of said instance
        write new structure to output file
    close both files
    remove old file (the one you just used for input)
    rename new file (the one you just used for output)
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    i dun really get it ... heres where i'm stuck at

    Code:
    struct loan {
    	char bookid[6], bookname[20], datedue[9],dateloan[9];
    }com;
    
    struct cus_info {
    	int id;
    	char name[40];
    	struct loan;
    }cus;
    
    void loan(void)
    {
    FILE *cusfile,*temp;
    int idd;
    
    temp=fopen("c:\\temp.dat","a");
    	if((cusfile=fopen("c:\\cus.dat","r+")) == NULL)
    			{
    				gotoxy(17,5);
    				printf("Database Is Empty!");
    				getch();
    			}
    	else
    	{
    		gotoxy(17,5);
    		printf("Enter '0' To Return To Main Menu");
    		gotoxy(17,7);
    		printf("Enter ID : ");
    		scanf("%d",&idd);
    		if(id == 0)
    		{
    		return;
    		}
    		while(!feof(cusfile))
    		{
    				fread(&cus,sizeof cus,1,cusfile);
    			if(feof(cusfile))
    				break;
    					if(id != cus.cus_id)
    				fwrite(&cus,sizeof cus,1,temp);

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
    struct loan {
    	char bookid[6], bookname[20], datedue[9],dateloan[9];
    }com;
    
    struct cus_info {
    	int id;
    	char name[40];
    	struct loan book; /* or whatever you like to call it */
    }cus;
    :wq

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Magicz68, there are a few things wrong with your code besides the crucial error viaxd pointed out (you have to name the struct inside the struct). Why do you only check the return value of fopen() for one file, not both? You also need to check the return value of scanf() if you are going to use it. You shouldn't use feof() to control a loop either, read here: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 05:07 AM
  3. fread struct with unknown size
    By ReggieBE in forum C Programming
    Replies: 12
    Last Post: 03-27-2006, 12:19 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21