Thread: Reading structures

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Reading structures

    Right heres my problem I'm making an address book. The information about contacts gets saved into this struc
    Code:
    struct data
    {
        char fname [51],sname [51],tel[15];   
    }a;
    Then saved into a binery file. The problem happends when I close the program then open it again and open a address book to view it. The program dosen't know how many contacts are saved so it can't open them all.

    Can anyone help me please
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    After you've written to the file and your app has closed, is there anything in the file? Eg open it with your editor and have a look. If there's nothing in there either the write failed, or maybe you forgot to fclose() the stream.

    Otherwise, post some code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    OK

    Right I checked in the file and yes it does have stuff in there. So hers my entrie code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void main_menu(); /*main_menu prototype*/
    void stop(); /*stop prototype*/
    void clrsf(); /*clrsf prototype*/
    void print_header(); /*print_header prototype*/
    void new_book(); /*new_book prototype*/
    void open_book(); /*open_book prototype*/
    void view_edit(); /*view_edit prototype*/
    int create_entry(); /*create_entry prototype*/
    int view_book(); /*view_book prototype*/
    void exit_app(); /*exit_app prototype*/
    
    
    struct data
    {
        char fname [51],sname [51],tel[15];             /*fname=forename, sname=surename, tel=tel. number.*/
        int x;                                          /*x=no. entrys*/
    
    }a;
    
    FILE *f;
    char fn[104],y_n;
    int i=0;
    int t;
    
    /*This creates a pause in the program*/
    void stop()
    {
        system("PAUSE");
    }
    
    /*This function clears scanf*/
    void clrsf()
    {
        while ( getchar() != '\n' );
    }
    
    /*Program infomation is printed when this function is called*/
    void print_header()
    {
        printf("\nDG's address book\n");
        printf("by Daniel Granger\n");
        printf("Version 1.0 BETA, released 13/09/02\n");
    }
    
    void new_book()
    {
    char a;
        clrscr(); /*clear the screen*/
    b1:
        printf("What do you want your new address book to be called: "); /*print the text onto the screen*/
        scanf("%s", &fn); /*get input from user for address book name*/
    a1:
        printf("\n\nAre you sure you want to call your new address book '%s'. (y)es or (n)o: ", fn); /*prints text also with the file name in 'fn'*/
        clrsf();
        scanf("%c", &a); /*gets input from user to see if they want the file name*/
        if (a=='y')/*if user typed yes*/
        {
            strcat( fn, ".dg" ); /*add .dg onto the user's filename*/
            stop();
            main_menu();
        }
        else if (a=='n')/*if user typed no*/
        {
            printf("\nOk then... ");
    goto b1; /*up*/
        }
        else /*if user typed somthing else*/
        {
            printf("\nIts either y(yes keep the filename) or n(I dont want that file name, let me \nchoose anorther)");
    goto a1; /*up*/
        }
    
    }
    
    void open_book()
    {
    char a;
        clrscr(); /*clear the screen*/
    b2:
        printf("Whats the name of the address book you want to open: ");
        scanf("%s", &fn);
    a2:
        printf("\nAre you sure you want to open the address book '%s'. (y)es or (n)o: ");
        clrsf();
        scanf("%c", &a);
        
        if (a=='y')/*if user typed yes*/
        {
            strcat( fn, ".dg" );
            
            if ( (f=fopen(fn,"rb"))== NULL)
            {
                    printf("\nUnable to access address book\n");
                    stop();
                    fclose(f);
    goto b2; /*up*/
            }
        }
        else if (a=='n')/*if user typed no*/
        {
            printf("\nOk then... ");
    goto b2; /*up*/
        }
        else /*if user typed somthing else*/
        {
            printf("\nIts either y(yes open that address book) or n(I dont want to open that address \nbook)");
    goto a2; /*up*/
        }
        
        stop();
        main_menu();
    
    }
    
    void view_edit()
    {
    int a;
    
        clrscr();
        printf("Do you want to create a new entry(1) or view the address book(2): ");
        
        scanf("%d", &a);
        if (a==1) /* if user typed 1 NEW ENTRY*/
        {
        create_entry();
        }
        else if (a==2)
        {
        view_book();
        }
        else /*if user doesn't type 1 or 2*/
        {
        main_menu();
        }
    }
    
    int create_entry()
    {
    
    
        clrscr(); /*clear the screen*/
        printf("\t\t\tCreate Entry\n\n"); /* title*/
    a3:
        printf("whats the contacts forename: "); /*print text to screen*/
        clrsf();
        fgets(a.fname, 51, stdin); /*gets contacts forename*/
        printf("whats the contacts surname: "); /*print text to screen*/
        fgets(a.sname, 51, stdin); /*gets contacts surname*/
        printf("whats the contacts telphone number: "); /*print text to screen*/
        fgets(a.tel, 15, stdin); /*gets contacts telephone number*/
    b3:
        printf("\n\nIs the following information correct and do you want to save it?\n"); /*print text to the screen*/
        printf("Forename: %s", a.fname); /*print text to the screen with contacts forename*/
        printf("Surname: %s", a.sname); /*print text to the screen with contacts surname*/
        printf("Telphone number: %s", a.tel); /*print text to the screen with contacts telephone number*/
        printf("\n\ny or n: "); /*print text to the screen*/
        scanf("%c", &y_n); /*ask yes or no*/
        clrsf();
        if(y_n=='y')/*yes*/
        {
            a.x++; /*add one to entrys*/
            f=fopen(fn,"a"); /*open file 'fn' and write at last point in the struc*/
            if (!f) /*if it can't open the file*/
                    return 1; /*return 1 and quit*/
            fwrite(&a, sizeof(struct data),1,f); /*copy struct data into the binary file*/
            fclose(f); /*close file*/
            
    
            stop();
            main_menu();
        }
        else if (y_n=='n')/*no*/
        {
            printf("\nOk then... ");
    goto a3; /*up*/
        }
        else /*if user typed anything apart from y or n*/
        {
            printf("It was y(yes the infomation is correct and you want to save it) or n(no the information is not correct dont save it)");
    goto b3; /*up*/
        }
    }
    
    int view_book()
    {
        clrscr();
        printf("\t\t\tEntry view\n"); /* title*/
    
        f=fopen(fn,"r"); 
        if (!f)
            return 1;
    
        t=a.x;
    
        while (i<t)
        {
            i++;
            fread(&a,sizeof(struct data),1,f);
            printf("Forename: %s", a.fname); /*print text to the screen with contacts forename*/
            printf("Surname: %s", a.sname); /*print text to the screen with contacts surname*/
            printf("Telphone number: %s", a.tel); /*print text to the screen with contacts telephone number*/
            printf("-__________________________________-\n");
        }
        
        fclose(f);
        i=0;
        
        stop();
        main_menu();
    }
    
    void exit_app()
    {
        clrscr();
        printf("\n\n\t\tBYE\n\n");
        printf("Thank you for using DG's address book.\n");
        stop();
    }
    
    
    
    /*This is the main menu you get at the beggining of the appilcation*/
    void main_menu()
    {
    int a;
        clrscr();
    mm_begining:
        printf("What do you want to do?\n\n");
        printf("1. Create new address book.\n");
        printf("2. Open exsiting address book\n");
        printf("3. View/edit current book.\n");
        printf("4. Exit\n\n");
        
        printf("Please type the number of the options you wish to execute then press enter:-");
        
        scanf("%d", &a);
        clrsf();
        
        if (a==1)
            new_book();
        else if (a==2)
            open_book();
        else if (a==3)
            view_edit();
        else if (a==4)
            exit_app();
        else
        {
            printf("\nsorry I didn't understand please can you choose an option 1-6\n\n");
            goto mm_begining;
        }
    }
    
    
    
    
    
    
    int main()
    {
    a.x=0;
    main_menu();
    
    return 0;
    }
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Thats a fair bit of code to plough through!

    Here's a couple of things to start with:

    - f=fopen(fn,"a");
    This is not opening the file in binary mode, which is needed if you're fwrite()'ing struct to it. The same goes for the fopen() on the read side of things. Have a look at this.

    - goto
    yak, there's a lot of goto's in there. You'd do yourself a big favour by removing them
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    Umm I thought (f=fopen(fn,"a") the "a" opened the file so when you write to it writes at the end of the file.

    It says that here

    a
    Open an existing file (or create a new one) for writing. The file pointer is positioned to the end of the file before every write.
    Also whats wrong with the goto's they work. What should I do instead of that. Also do you have away to read all the entries in the file
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    If you use mode "a" this defaults to a text file, the same as if you use "at". Therefore you've opened your binary file as a text file. You need to use "ab".

    I'm sure this is right but someone will correct me if I'm wrong.

    hobo

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Granger9


    Also whats wrong with the goto's they work.
    in 2.74MB of source from a professionally-written program i have sitting in front of me, there are two gotos. why? because goto's belong in BASIC. not c.
    hello, internet!

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    Yeah ab works but so did a and they seem to do the same thing I can't understand the diffrence
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    But

    But goto's work they don't seem to casue a problem (which I can see) and they seem an easy way of cutting down on code
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    they contribute to creation of hard to read, hard to maintain code. DONT USE THEM.
    hello, internet!

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    Right ok. I trust you lot once I have got this problem sorted I will get rid of them.

    But can anyone please help with my First question in this thread
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, I'm off in a bit, but I'll have another look tonight and help you if someone else hasn't already.

    <like your sig, btw >
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>scanf("%s", &fn);
    This is wrong, it should be
    >scanf("%s", fn);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    If you're going to use goto, at least jump forward in the code instead of backward. If you jump backward it makes the program harder to follow.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  15. #15
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    This should make your life easier

    I have got rid of all the usless stuff in the previous post to give you exactly want you you need to solve my problem.

    Code:
    #include <stdio.h>
    
    struct rec
    {
        int the;
        int x;
    }r;
    
    FILE *f;
    int i,q;
    
    int main()
    {
    r.x=0;
    printf("how many");
    scanf("%d", &i);
    
    
    f=fopen("open_read","w");
        if (!f)
            return 1;
    for (q=1;q<=i; q++)
    {
    r.the=q;
    r.x++;
    fwrite(&r,sizeof(struct rec),1,f);
    }
    fclose(f);
    
    return 0;
    }
    I have added r.x to count the number of entries but into the file if you could read the last entry of r.x it would have the number of entries so then you would be able to make a loop like "for(q=1; q<=r.x; q++)" then read all then entrys. Thats how I would do it but i don't know how to read the last r.x.

    If you would do it diffrently please do but can someone make a program which would read all the entrys, please it would be a great help.

    I hope this helped you all in understanding what I'm trying to achive.
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  5. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM