Thread: File I/O question?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    9

    File I/O question?

    Here is the sample code from this site in the tutorial....

    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
    }
    Where does the "this" text come from, that forms in the program window?

    May be a stupid question but I have read this code over and over and cant seem to find where it originates from....

    Thanks...
    -Fabrik8

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The sentence "This text will now be inside of example.txt" is output to example.txt in the first few lines of code (using a_file).

    Then example.txt is opened for input (using b_file) and the first word is read in. operator>> stops at the first whitespace after a non-whitespace character, meaning it reads only one word and stops at a space. That is why it reads in only the first word in the file, which happens to be "This".

    The code then outputs that word.

    BTW, str should really be a string, not a character array, but this code will work either way.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    Quote Originally Posted by Daved
    The sentence "This text will now be inside of example.txt" is output to example.txt in the first few lines of code (using a_file).

    Then example.txt is opened for input (using b_file) and the first word is read in. operator>> stops at the first whitespace after a non-whitespace character, meaning it reads only one word and stops at a space. That is why it reads in only the first word in the file, which happens to be "This".

    The code then outputs that word.

    BTW, str should really be a string, not a character array, but this code will work either way.

    Thanks "Daved" I replaced the text with "This_is_where_the_text_will_output" for my notes and entered a /*note of space stop*/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Another dumb question about file i/o
    By Cobras2 in forum C++ Programming
    Replies: 23
    Last Post: 03-14-2002, 04:15 PM