Thread: write(), read()

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    23

    write(), read()

    Hi there. I want to save the inventory in a txt file. Am using structures . When the user chooses to save in the menu i want to write the whole inventory to a txt file.

    my structure looks like this
    Code:
     
     struct inventory
       {
        char title[25];
        char pub_date[9];
        char author[20];
        int num;
        int on_order;
        float retail;
       };
    The function witch is supposed to save the invenoty looks like this
    Code:
    int save(inventory book[1000], int total)
    {
       fp.open("books.txt", ios::in | ios::out );
       fp.write((unsigned char *)&book, sizeof(inventory));
       fp.close();
       return 0;
    }
    This doesn't show any errors but when i open the txt file it shows
    some garbage characters. Also the read() command doesnt work.
    Am using the write() comand correctly?
    If please someone can show me an example of write(), and read()
    with structures it would really help me.
    attached the whole code if enyone wants to look at it. (book inventory program witch i just added the save function to test)

    Thanx for your help.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You may want to overload the << and >> operators (or write an equivilent function) and write the struct as text fields. When reading, the stream will handle the appropriate type conversions (so the string "255" can be read into an int) .

    You're probably getting garbage in places because the struct's members will be alligned on some multiple of a byte boundary for speed and the compiler is inserting padding.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    23
    Sorensen you lost me there. Am sorry its just am new to c++. If you can show me an example of this or anywere i can get some more info, it would help me. Thanxs.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Code:
    struct inventory
       {
        char title[25];
        char pub_date[9];
        char author[20];
        int num;
        int on_order;
        float retail;
    
        void write(ostream& os)
        {os<<title<<' '<<pub_date<<' '<<author<<' '<<num....etc}
    
        void read(ifstream& is)
       {is>>title>>pub_date...etc}
    };
    Then create your ofstreams and ifstreams and send them to the read/write member functions.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fp.open("books.txt", ios::in | ios::out );
    If you use write() to store the inventory, you should use binary mode to open the file. Same thing when you use read().
    fp.open("books.txt", ios::in | ios::out | ios::binary );

    > fp.write((unsigned char *)&book, sizeof(inventory));
    This only writes one record. To write the whole array use:
    fp.write((unsigned char *)&book, total*sizeof(inventory));

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    23
    Thanxs everyone . This really helped me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read write problem
    By zephon in forum C Programming
    Replies: 3
    Last Post: 01-13-2009, 04:04 AM
  2. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  3. read and write to file
    By rahulsk1947 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2007, 09:56 PM
  4. using threads to write & read from files
    By kishorepalle in forum C Programming
    Replies: 4
    Last Post: 10-19-2004, 05:19 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM