i/o problem...

This is a discussion on i/o problem... within the C++ Programming forums, part of the General Programming Boards category; Im new to C++, I no some basic, but changed language because basic was a bit too basic, anyway, I ...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question i/o problem...

    Im new to C++, I no some basic, but changed language because basic was a bit too basic, anyway, I have a problem when making i/o files. I think the problem is the way that I open the files, because I use the "std::cin >> details;" and "cin.getline(details,256,'\n');" and "a_file << details;" in the source code, but when u try and open the file, it only shows the first word in the file. This is part of my source code, please tell me what you would do in this position...

    WHEN MAKING THE FILE:-

    Code:
    void
    create ()
    {
      cout << "                     Please enter the ID number that you want to" << endl;
      cout << "                               Assign to this candidate" << endl;
      cout << "                              Candidate reference number:";
      cin >> ID;
      ofstream a_file(ID);
    
      cout << "" << endl;
      cout << "            Please input the data that you want to hold about this client" << endl;
      cout << "               Please begin the entry with a // and a space before typing" << endl;
      cout << "                                in your database entry" << endl;
      cout << "                              Please input the data here..." << endl;
      cout << "" << endl;
      std::cin >> details;
      cin.getline(details,256,'\n');
          a_file << details;
    CODE WHEN OPENING THE FILE:-

    Code:
    void
    view ()
    {
        cout << "" << endl;
        cout << "             Please enter the reference number of the database file:" << endl;
        cin >> ID;
        cout << "" << endl;
    
        ifstream b_file(ID);
        cout << "" << endl;
        b_file >> details;
        cout << details;
    Any ideas...

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    a_file << details;
    will only write the first thingie...

    Try

    Code:
    a_file.write(details, strlen(details));
    also

    Im a littled worried about this...

    Code:
    std::cin >> details;
      cin.getline(details,256,'\n');
    I am pretty sure you could just use the cin.getline method

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Nice try, but That doesn't change anything, the file still does the same thing, it still only shows the first word of the file, not the entire entry, not bad idea though, any more???


  4. #4
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    If you can do it using the getline method, then what do you need to do, maybe I'm missing something else in my source code...

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    There is nothing wrong with a_file << details. The problem is with:
    b_file >> details;

    That will only read up to the first space in the file. Use b_file.getline(details, 256); instead.

    Alternately, you could use std::string so that you won't be limited to 256 characters (or whatever size you're using):
    Code:
    WHEN MAKING THE FILE:-
     
    void
    create ()
    {
    cout << " Please enter the ID number that you want to" << endl;
    cout << " Assign to this candidate" << endl;
    cout << " Candidate reference number:";
    cin >> ID;
    ofstream a_file(ID);
     
    cout << "" << endl;
    cout << " Please input the data that you want to hold about this client" << endl;
    cout << " Please begin the entry with a // and a space before typing" << endl;
    cout << " in your database entry" << endl;
    cout << " Please input the data here..." << endl;
    cout << "" << endl;
    std::string details;
    std::cin >> details;
    //cin.getline(details,256,'\n');
    std::getline(cin, details);
    a_file << details;
     
     
    CODE WHEN OPENING THE FILE:-
     
    void
    view ()
    {
    cout << "" << endl;
    cout << " Please enter the reference number of the database file:" << endl;
    cin >> ID;
    cout << "" << endl;
     
    ifstream b_file(ID);
    cout << "" << endl;
    std::getline(b_file, details);
    cout << details;
    **EDIT** If you're using Microsoft Visual C++ 6.0, there's a glitch with std::getline that you need to fix yourself - see this article for details.
    Last edited by Hunter2; 08-31-2004 at 11:53 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file I/O
    By Neo1 in forum C++ Programming
    Replies: 6
    Last Post: 12-01-2006, 11:44 PM
  2. Newbie problem: I/O File
    By Ardetcho in forum C Programming
    Replies: 4
    Last Post: 07-19-2006, 04:27 PM
  3. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  4. Binary File I/O Problem
    By 0rion in forum C Programming
    Replies: 10
    Last Post: 08-31-2004, 12:53 PM
  5. Interesting problem with file I/O...
    By skillet in forum C++ Programming
    Replies: 12
    Last Post: 02-20-2002, 10:14 AM

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