File Save and Load

This is a discussion on File Save and Load within the C Programming forums, part of the General Programming Boards category; Hi, I am currently writing a code were the user is asked to enter details for the number of people ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    19

    File Save and Load

    Hi,

    I am currently writing a code were the user is asked to enter details for the number of people they require. It's also required that the data must be saved, and then reloaded again to be displayed on the screen. However, when reloading the data, 'Oh dear' appears which is part of the if funtion, which should only appear if something is wrong with the file_load or file_save function. Here is the current code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define name_length 20
    
    struct Details
    	{
       char surname[name_length];
       char forename[name_length];
       int birth_year;
       int birth_month;
       int birth_day;
       };
    
    struct Details get_details(int);
    void print_details(struct Details);
    int file_save(int, char [], struct Details *);
    int file_load(char [], struct Details *);
    
    int main (void)
    {
    unsigned i,s;
    struct Details *p;
    printf("How many sets of details?");
    scanf("%u",&s);
    
    if((p=(struct Details *)malloc(s * sizeof(struct Details)))==NULL)
    	{
       printf("Cannot allocate %u bytes",s);
    	getch();
       return 1;
       }
    
    for (i=0;i<s;i++)
    	{
       p[i]=get_details(i);
       }
    printf("Class details:\n");
    for (i=0;i<s;i++)
    	{
       print_details(p[i]);
       }
    printf("\n\nHit a key");
    getch();
    if((file_save(s,"datafile.txt",p))==0);
        {
       printf("Oh dear!\n");
       }
    
    s=file_load("datafile.txt",p);
    if((s)==0)
    	{
       printf("Oh dear!\n");
       }
    
    for (i=0;i<s;i++)
    	{
       print_details(p[i]);
       }
    printf("\n\nHit a key");
    getch();
    free(p);
    return 0;
    }
    
    struct Details get_details(int i)
    {
    struct Details get_member;
    
    printf("\nDetails for class member %d",i+1);
    printf("\nEnter the forename of the class member:");
    scanf("%s",get_member.forename);
    printf("\nEnter the surname of the class member: ");
    scanf("%s",get_member.surname);
    printf("\nEnter the year of birth of the class member: ");
    scanf("%d",&get_member.birth_year);
    printf("\nEnter the month of the birth of the class member: ");
    scanf("%d",&get_member.birth_month);
    printf("\nEnter the say of birth of the class member: ");
    scanf("%d",&get_member.birth_day);
    
    return get_member;
    }
    
    void print_details(struct Details print_member)
    {
    printf("\n %s %s \t%2d-%2d-%4d",print_member.forename,print_member.surname,print_member.birth_day,print_member.birth_month,print_member.birth_year);
    }
    
    int file_save(int size,char filename[],struct Details *fileptr)
    {
    int i;
    FILE *fptr;
    if((fptr=fopen(filename,"wb"))==NULL)
    	{
       printf("File cannot be opened\n");
       exit(1);
       return 0;
       }
    fwrite(fileptr,sizeof(struct Details),size,fptr);
    fclose(fptr);
    return 1;
    }
    
    int file_load(char filename[],struct Details *fileptr)
    {
    FILE *fptr;
    int size;
    realloc(fileptr,size);
    if((fptr=fopen(filename,"rb"))==NULL)
        {
        printf("File cannot be opened\n");
        exit(1);
        return 0;
        }
    fread(fileptr,sizeof(struct Details),size,fptr);
    fclose(fptr);
    return 1;
    }
    I have highlighted the areas in which i think the problem lies. Can someone point me in the right direction please?

    Thanks, Luke.
    Last edited by lukesowersby; 05-12-2009 at 12:19 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    For starters, please indent your code properly. This can help you detect bugs due to typographical errors that result in the control flow being not quite what you expected.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Huh?
    Code:
    int file_load(char filename[],struct Details *fileptr)
    {
        int size;
        realloc(fileptr,size); /* <-- size is what here? */
        if((fptr=fopen(filename,"rb"))==NULL)
        {
            printf("File cannot be opened\n");
            exit(1);
            return 0; /* <--- Never reached, you exited a line earlier
        }
        fread(fileptr,sizeof(struct Details),size,fptr); /* What could size be here? */
        fclose(fptr);
        return 1;
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    you have lost the return value of realloc
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-17-2008, 02:29 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  3. Replies: 3
    Last Post: 09-12-2005, 09:08 AM
  4. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  5. How to save into file ?
    By Th3-SeA in forum C Programming
    Replies: 7
    Last Post: 11-15-2003, 05:54 PM

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