Thread: File Writing/Reading system

  1. #1
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156

    File Writing/Reading system

    Hi again.

    Since i've bypassed the login system, i've not tried to create a simple reading and writing to a file system to keep track of business records.

    Here are the code snippets:

    Code:
    std::string DatabaseMgr::sRecordData()
    {
         std::system("cls");
         std::ofstream File_Input("Record.txt", std::ios::app);
         std::cout << "Welcome, please record the data as follows:" << std::endl;
         std::cout << "\nNote! Do not place spaces inbetween words when recording\nnew products, this will crash the application!" << std::endl;
         std::cout << "\nProduct Purchased:";
         std::cin >> m_cProduct;
        /* std::cout << "\nDate the Transaction was made: ";
         std::cin >> m_sDatePurchased;
         std::cout << "\nQuantity of the Purchased Item: ";
         std::cin >> m_iQuantity;*/
         std::cin.getline(m_cProduct, 50);
         File_Input << m_cProduct << "\n";
         File_Input.close();
         std::cout << "Thank you for recording!" << std::endl;
         vReturnToStart();
    }
    ..that is the record data function, and here is the read data funcion:

    Code:
    void DatabaseMgr::vReadData()
    {
         std::system("cls");
         std::cout << "Here are the records you requested: " << std::endl;
         std::ifstream File_Output("Record.txt");
         while(File_Output.get(m_cBuffer))
         {
              std::cout << m_cBuffer;
         }
         std::cout << "\n\nThank you for reading our records!" << std::endl;
         File_Output.close();
         std::cin.ignore();
         vReturnToStart();
    }
    I C&R'd the whole program with no errors, untill i get a run-time error!

    I can write to the file just fine, but my program will not let me read the text! I will post a screenie to elaborate.

    Can anybody tell me what i've done wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > std::ofstream File_Input("Record.txt", std::ios::app);
    > std::ifstream File_Output("Record.txt");
    An output file called "Input", and an input file called "Output" - confused yet?

    > std::cin >> m_cProduct;
    I bet this leaves a newline on the input stream.

    > std::cin.getline(m_cProduct, 50);
    > File_Input << m_cProduct << "\n";
    I bet this just reads the newline, and outputs a single \n to the file.

    Did you
    a) open the file in a text editor to see what you had?
    b) Modify your code to test whether anything was happening?
    Say
    Code:
     std::cout << "--" << m_cBuffer << "++" << endl;
    Depending on what (if anything) you saw of "--++", it would tell you instantly a lot about of what was going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Check what record.txt has... if it has text and you still cannot read it...
    Then use this method of reading instead:

    Code:
    ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          cout << line << endl;
        }
        myfile.close();
      }
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    execute, your example code is incorrect. eof() should not be used to control the loop. Use getline() itself to control the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Salem View Post
    > std:fstream File_Input("Record.txt", std::ios::app);
    > std::ifstream File_Output("Record.txt");
    An output file called "Input", and an input file called "Output" - confused yet?
    Yeah.. it's my way of remembering which is which

    Quote Originally Posted by Salem View Post
    > std::cin >> m_cProduct;
    I bet this leaves a newline on the input stream.

    > std::cin.getline(m_cProduct, 50);
    > File_Input << m_cProduct << "\n";
    I bet this just reads the newline, and outputs a single \n to the file.
    I bet your probably right.

    Quote Originally Posted by Salem View Post
    Did you
    a) open the file in a text editor to see what you had?
    b) Modify your code to test whether anything was happening?
    Yes I did, and there's nothing, just empty lines where the text should be

    Quote Originally Posted by Salem View Post
    Say
    Code:
     std::cout << "--" << m_cBuffer << "++" << endl;
    Depending on what (if anything) you saw of "--++", it would tell you instantly a lot about of what was going on.
    I did do that, and i'll show you what i got as a result :
    Last edited by legit; 11-24-2008 at 02:01 AM.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, you can then fill the empty lines with a text editor and test your vReadData function. Then if everything is ok you can test your sRecordData. So you can isolate what function causes the problem

  7. #7
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Not a bad idea, i'll get right on that :P

    It's definately the sRecordData function, as I can view the data when not recording it using the databse, thanks!
    Last edited by legit; 11-24-2008 at 02:42 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks to me like m_cBuffer is a single char, because all you got was a single space first, and then a newline.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Salem View Post
    Looks to me like m_cBuffer is a single char, because all you got was a single space first, and then a newline.
    It is, is that the problem?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well yeah, if you were expecting it to read a line it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    But he is reading character by character with ifstream::get

    If m_cProduct and/or m_iQuantity and/or m_sDatePurchased are char that is bad. But you would get something for an output.

    Character by character read should be done like this: http://www.cplusplus.com/reference/i...tream/get.html
    Though, according to the above, it shouldn't work for your, since it would return a reference to a File_Output, thus non-zero. So it would crash, which it doesn't
    (I feel that I have something wrong here)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM