File Writing/Reading system

This is a discussion on File Writing/Reading system within the C++ Programming forums, part of the General Programming Boards category; Hi again. Since i've bypassed the login system, i've not tried to create a simple reading and writing to a ...

  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?
    Attached Images Attached Images  

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    22,304
    execute, your example code is incorrect. eof() should not be used to control the loop. Use getline() itself to control the loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 :
    Attached Images Attached Images  
    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
    32,852
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,852
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10:13 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21