Thread: Having troubile with fstead::read

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    Having troubile with fstead::read

    so i've got my write function to work that saves the file.

    Code:
    ...
        fout.open("list.bin", std::ios::binary);
        for (int i = 0; i < m_TheList->getSize();i++){
            BillData billdat = m_TheList->getDat(i);
            fout.write((char*) &billdat, sizeof billdat);
        }
        fout.close();
    ...
    But i can't wrap my mind around the best way to do the oposite when the program starts, i'll post what i have! If it helps i'm trying to load data of "BillData" types into a variable, take it's member's and read them into the accessor for the vector?(InsertBill) of BillList (m_TheList),

    Code:
    fin.open("list.bin", std::ios::binary); //Load the list of bills.
        if (fin){ // if the file was found.
            //BillData *temp = new BillData(fin.read((char *) &temp, sizeof temp));
           // fin.read(&temp, sizeof temp);
            //temp = fin.read(char *) &temp, sizeof temp); 
            
             m_TheList->InsertBill(temp.Name,temp.Cost,temp.DueDate);  <-this is fine.
    
    
        }
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No need to use new here, use a local variable:
    Code:
    BillData temp;
    fin.read((char *)&temp, sizeof temp);

  3. #3
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    ok, so i have this as the read from file:

    Code:
    if (fin){ // if the file was found.
            BillData temp("","",wxDateTime::Now());
            fin.read((char *)&temp, sizeof temp);
            m_TheList->InsertBill(temp.Name,temp.Cost,temp.DueDate);
    
        }
    and BillData looks like

    Code:
    class BillData
    {
        public:
        wxString Name;
        wxString Cost;
        wxDateTime DueDate;
    
        BillData(wxString tempName, wxString tempCost, wxDateTime tempDate);
        ~BillData() {};
    };
    But the read function throws a segfault on the "inputBill" line.
    Sorry, but i'm a Code::Blocks man now.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are wxStrings POD types? I doubt they are. You can't write out the binary values of non-POD types directly like that.

    You can use text based reading and writing, which is usually sufficient. Or you can do tricks to output the data by outputting the size first and then the data. Either solution loses the speed of access that a binary file will give you, but it's hard to get that speed without sacrificing the ability to have different sized data.

    What was your reasoning for choosing binary I/O?

  5. #5
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Because i am writing BillData Objects to the file an not the info they hold, so it made sense to use binary.
    Sorry, but i'm a Code::Blocks man now.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Because i am writing BillData Objects to the file an not the info they hold, so it made sense to use binary.

    I don't understand what you mean. It sounds like you are saying that you wanted to write out the objects and not the data they hold. What is in a BillData object other than the data that it holds? Isn't the file supposed to hold the data?

  7. #7
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    er. sorry, i meant that i was trying to write out whole objects instead of explicitly writing out each value held by the objects.
    Sorry, but i'm a Code::Blocks man now.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok. I'm pretty sure that's not possible if the objects aren't POD.

    Using text based I/O and stream operators it shouldn't be that hard to do it member by member.

Popular pages Recent additions subscribe to a feed