#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    24
    Rep Power
    0

    C: file input and output


    Hello

    C problem
    I am having problems saving a structure to a text file. My structure is below.

    [code]
    //Hold details for all the customers
    struct freight{
    char name[20];
    char address[40];
    char goods[30];
    int quantity;
    char destination[30];
    } customerDetails[20];
    [\code]

    What is the best way to save this to a file and open it from a file and display it on the screen. I have tried several ways but can't get it to work propertly.

    I have tried using fread and fwrite, code is below.
    [code]
    if((fileCustomers = fopen(fileName, "wb")) == NULL)
    printf("\nCannot save this record");

    for(i=0; i<=recordNumber; i++){
    if(fwrite(&customerDetails[i], sizeof(struct freight),1,fileCustomers) != 1){
    printf("\nError saving customer records");
    exit(1);
    }
    }
    [\code]

    code for reading
    [code]
    if((fileCustomers = fopen(fileName, "rb")) == NULL){
    printf("\nCannot not find customer records on disk");
    printf("\nOr file may not have been created yet");
    }

    for(i=0; i<=2; i++){
    if(fread(&customerDetails[i],sizeof(struct freight),1,fileCustomers) != 1){
    printf("\nError opening customer records");
    exit(1);
    }
    }
    [\code]
    Many thanks in advance

    Steve
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Steve,

    How is the program failing? Is the file not getting created? Is that data that comes in garbage? Is it failing to find the file completely?

    Some details of the failure would help us out a lot.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    24
    Rep Power
    0
    I have the program working fine now. I will give you the details later if any one is trying to do the same thing.

    Thanks for your help
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    24
    Rep Power
    0
    The code is working ok now.

    But just one small thing, what is the best way to delete a customer from the structure.

    Many thanks in advance

    Steve
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    How you delete a record will be entirely dependent upon the container that you use to store the structure. What sort of container are you using?
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    24
    Rep Power
    0
    I am using a structure to store the customer details in.

    [code]
    //Hold details for all the customers
    struct freight{
    char name[20];
    char address[40];
    char goods[30];
    int quantity;
    char destination[30];
    } customerDetails[20];
    [\code]


    If l have a array of about 20 customers, and l want to delete customer 10. How would i delete it so that after l delete customer 11 would become customer 10. As all the customers following customer ten will be reduced by 1.

    Many thanks in advance.

    Steve
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,214
    Rep Power
    2222
    Originally posted by steve_rm
    I am using a structure to store the customer details in.

    [code]
    //Hold details for all the customers
    struct freight{
    char name[20];
    char address[40];
    char goods[30];
    int quantity;
    char destination[30];
    } customerDetails[20];
    [\code]


    If l have a array of about 20 customers, and l want to delete customer 10. How would i delete it so that after l delete customer 11 would become customer 10. As all the customers following customer ten will be reduced by 1.

    Many thanks in advance.

    Steve
    [mild sarcasm]You're going to love this[/mild sarcasm] . You will need to copy every single record to the right (ie, above) customer 10 to the left one place.; ie, #11 -> #10, #12 -> #11, etc. You can easily implement this with a for-loop.

    Since you are dealing with 142+ byte records, this involves a bit of work. If your array were much larger, it would be even worse. You can live with it in this case, but sometime you might want to investigate creating an array of pointers to struct freight. That way, to move a record to a different place in the array, you just copy the pointer (what? 4 bytes?).

    OBTW, the end-code tag uses a / , not a \ .
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    This is one of those situations where you might want to use a vector instead of an array. Vectors include a method for removing unwanted members, and make adding members easy, without having to know how many there are ahead of time.

    Worth looking into.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo