Thread: File I/o

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Question File I/o

    Hello, I don't know if its my fault or the author's who wrote the tutorial from where I copied this code, but I can't understand why it prints only "This" instead of the whole text in the txt file ?

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[10];
    
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "example.txt" );
      // Outputs to example.txt through a_file
      a_file<<"This text will now be inside of example.txt";
      // Close the file stream explicitly
      a_file.close();
      //Opens for reading the file
      ifstream b_file ( "example.txt" );
      //Reads one string from the file
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }
    Thank you

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The >> operator is reading into the array str[]. I believe it's working as designed. Perhaps you want to read a line at a time, instead of reading a "string" at a time.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    What do I do to get "This text will now be inside of example.txt" printed ?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, first off, since you are using C++, I would suggest using a string instead of a char array. Strings can grow dynamically and you don't have to jack with storage limits.

    Then, you can read a line at a time, or, read a "word" at a time in a loop until EOF is hit.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    I incresed the size if str to str[50]


    Please tell me where I am going wrong:

    I think this
    Code:
    b_file>> str;
    should put everything inside "example.txt" into str (I mean the whole sentence)


    and this
    Code:
    cout<< str <<"\n";
    should display it on the screen ?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >> stops at whitespace.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    But this
    Code:
    cout<<"Hello World";
    dosen't stop at whitespace ? Maybe I should start all over again

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    ok so the problem was with
    Code:
    b_file>>str;
    and not with cout. b_file ignores everything after whitespace. Thanks.

  10. #10
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    I replaced it with
    Code:
    b_file.getline(str,49)
    and its working.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    If you use std::string which is found in <string> then you can do...
    Code:
    std::getline(b_file,str);
    That code will get the text (or binary) from file until '\n' is found. This is of course preference, but I personally find it quite helpful when you know for sure than there will be no errors in the file. Also note, that there can be your own terminating character which is the third argument.
    Code:
    std::getline(b_file,str);
    //would be the same as
    std::getline(b_file,str,'\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM