Thread: Writing to the file!

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

    Writing to the file!

    Why does this tell me i'm missing ")"'s?

    p.s. Will it work? I'm not sure how to use ofstream great?

    Code:
    void fincal2Frame::OnClose(wxCloseEvent &event)
    {
        theBar->RemoveIcon();
        fout.open("list.bin");
        for (int i = 0; i < m_TheList->getSize();i++){
            BillData billdat = m_TheList->getDat(i);
            fout.write((char*)&billdat.Cost, sizeof wxString);
            fout.write((char*)&billdat.DueDate, sizeof wxDateTime);
            fout.write((char*)&billdat.Name, sizeof wxString);
        }
        fout.close();
        Destroy();
    }
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Why does this tell me i'm missing ")"'s?
    >sizeof wxString
    I think you need sizeof(wxString), only because wxString is a class, or you could also write sizeof billdat.Cost.

    >p.s. Will it work?
    Probably not. Typically writing classes to a file as binary will not work, this only works for simple types. It may write something, but you won't be able to read it later. Also note that if you do write to a file as binary, you should open the file in binary mode.

    It's likely using write() won't work, so you'll probably have to write out the actual data instead, separated by either newlines, or some other delimiter like a space:
    Code:
            fout << billdat.Cost << "\n";
            fout << billdat.DueDate << "\n";
            fout << billdat.Name << "\n";

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Classes should serialize themselves. Just do
    fout << billdat << "\n";
    Overload operators for your class and let it handle the serialization of itself.
    This is due to the fact that many times you can't access private members of classes and you don't know what classes may store, what type of data, so if you just store it all, you may corrupt the data they get back.
    The class knows itself best, so let it handle read/write.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Ok so my write function works!

    Code:
    void fincal2Frame::OnClose(wxCloseEvent &event)
    {
        theBar->RemoveIcon();
        fout.open("list.txt");
        for (int i = 0; i < m_TheList->getSize();i++){
            //BillData billdat = m_TheList->getDat(i);
            /*
            fout.write((char*)&billdat.Cost, sizeof wxString);
            fout.write((char*)&billdat.DueDate, sizeof wxDateTime);
            fout.write((char*)&billdat.Name, sizeof wxString);*/
            fout << m_TheList->getCost(i) << "\n";
            fout << m_TheList->getDate(i)<< "\n";
            fout << m_TheList->getName(i) << "\n";
        }
        fout.close();
        Destroy();
    }
    and outputs to a file like

    Code:
    12
    20
    The Name
    but i'm having trouble getting it back into the program when it starts. This is what i have so far, but it doesn't work right.

    Code:
        fin.open("list.txt"); //Load the list of bills.
        if (fin && fin.peek()){ // if the file was found.
            while (!fin.eof() ){
            wxString TempName, TempCost;
            int TempDate;
            char buffer[256],buffer2[256];
           // BillData temp("","",wxDateTime::Now());
            fin >> buffer;
            TempCost = _T(buffer);
            fin >> TempDate;
            fin >> buffer2;
            TempName = _T(buffer);
            m_TheList->InsertBill(TempName,TempCost,TempDate);
            }
    
        }
        fin.close();
    Sorry, but i'm a Code::Blocks man now.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    fin >> buffer2;
    That only reads one word. Plus char arrays are vulnerable to overflow, so I'd avoid them alltogether. Use std::string, and getline() to get the text data. The numerical stuff, you can use >> my_integer, etc. (Why are you keeping cost in a string?)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by Cactus_Hugger View Post
    Code:
    fin >> buffer2;
    That only reads one word. Plus char arrays are vulnerable to overflow, so I'd avoid them alltogether. Use std::string, and getline() to get the text data. The numerical stuff, you can use >> my_integer, etc. (Why are you keeping cost in a string?)

    so somthing like this? I'm confused on how getline works?

    Code:
        fin.open("list.txt"); //Load the list of bills.
        if (fin && fin.peek()){ // if the file was found.
            while (!fin.eof() ){
            std::string TempName, TempCost;
            int TempDate;
            //char buffer[256],buffer2[256];
           // BillData temp("","",wxDateTime::Now());
            fin.getline(TempCost,256,'\n');
            fin >> TempDate;
            fin.getline(TempName,256,'\n');
            m_TheList->InsertBill(TempName,TempCost,TempDate);
            }
    
        }
        fin.close();
    Sorry, but i'm a Code::Blocks man now.

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Sorry - I didn't even know there was a member getline function. There's another : std::getline(istream& is, string& s, char delimiter = '\n'), so:
    Code:
    std::string TempName;
    std::getline(fin, TempName);
    // Convert TempName to a wxString (wxString has a constructor for this)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    ok so i have..

    Code:
        fin.open("list.txt"); //Load the list of bills.
        if (fin && fin.peek()){ // if the file was found.
            while (!fin.eof() ){
            std::string TempName, TempCost;
            int TempDate;
            //char buffer[256],buffer2[256];
           // BillData temp("","",wxDateTime::Now());
            std::getline(fin,TempCost);
            fin >> TempDate;
            std::getline(fin, TempName);
            m_TheList->InsertBill(TempName,TempCost,TempDate);
            }
    
        }
        fin.close();
    but this is causeing a crash now?
    Sorry, but i'm a Code::Blocks man now.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For one thing you shouldn't control the input loop with !eof(). Read the FAQ.

    And if TempDate is supposed to be on a separate line you might also have the "input stream leaving newline in the buffer" issue.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Ok it almost works;

    write:
    Code:
    void fincal2Frame::OnClose(wxCloseEvent &event)
    {
        theBar->RemoveIcon();
        fout.open("list.txt");
        fout << m_TheList->getSize() << "\n"; // write the number of units of data to the top of the file
        for (int i = 0; i < m_TheList->getSize();i++){
            fout << m_TheList->getCost(i) << "\n";
            fout << m_TheList->getDate(i)<< "\n";
            fout << m_TheList->getName(i) << "\n";
        }
        fout.close();
        Destroy();
    }
    and then the read:
    Code:
    
    
        fin.open("list.txt"); //Load the list of bills.
        if (fin && fin.peek()){ // if the file was found.
            int templength;
            fin >> templength;
            fin.ignore(1);
            for(int i=0; i<templength;i++ ){
            std::string TempName, TempCost;
            int TempDate;
            //char buffer[256],buffer2[256];
           // BillData temp("","",wxDateTime::Now());
            std::getline(fin,TempCost);
            fin.ignore(1);
            fin >> TempDate;
            fin.ignore(1);
            std::getline(fin, TempName);
            fin.ignore(1);
            m_TheList->InsertBill(TempName,TempCost,TempDate);
            }
    
        }
        fin.close();
    although it seems that the date is getting garbled when it's getting read if it's 2 digits long, the second digit is all the makes it in (i.e. 13 becomes 3)
    Sorry, but i'm a Code::Blocks man now.

  11. #11
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You call fin.ignore(1), which skips the first digit?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Call ignore only after calls to operator>>, not after calls to getline. The call to getline ignores the trailing newline automatically.

  13. #13
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by Daved View Post
    Call ignore only after calls to operator>>, not after calls to getline. The call to getline ignores the trailing newline automatically.
    ahhh
    Sorry, but i'm a Code::Blocks man now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM