Thread: fstream

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    fstream

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      char str[100];
    
      
      ofstream a_file ( "example.txt" );
      
      a_file<<"This text will now be inside of example.txt";
      
      a_file.close();
      
      ifstream b_file ( "example.txt" );
      
      
      b_file>> str;
      
      cout<< str <<"\n";
      cin.get();    
     
    }


    why is just prints "this", it supposed to print "This text will now be inside of example.txt".
    is there any solution for this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because reading a string from a file it stops at whitespaces.

    I beleive if you set "noskipws" on your iostream line, it will not do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    1337
    Join Date
    Jul 2008
    Posts
    135
    how to set that? is it something like

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      char str[100];
    
      
      ofstream a_file ( "example.txt" );
      
      a_file<<noskipws<<"This text will now be inside of example.txt";
      
      a_file.close();
      
      ifstream b_file ( "example.txt" );
      
      
      b_file>> str;
      
      cout<< str <<"\n";
      cin.get();    
    }
    if this is the case, i still get the "this", not the full string.
    Last edited by valthyx; 07-22-2008 at 10:16 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The noskipws is used in conjunction with the ifstream object, not the ofstream object.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To read a line of input into a string std::getline is used:

    Code:
    getline(b_file, str);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    1337
    Join Date
    Jul 2008
    Posts
    135
    none of the above works

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Or chances are you messed up..... again.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> none of the above works
    anon's suggestion assumes you're using C++ strings, but your code uses C style strings. Either switch to C++ strings or lookup getline for C style strings.

  9. #9
    1337
    Join Date
    Jul 2008
    Posts
    135
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      char str[10];
    
      
      ofstream a_file ( "example.txt" );
      a_file<<"This text will now be inside of example.txt";
      
      a_file.close();
      
      ifstream b_file ( "example.txt" );
      
      
      b_file>>noskipws>>str;
      
      cout<<noskipws<< str <<"\n";
      cin.get();    
     
    }

    didnt work

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is correct, I just tried it - but getline should definitely work. Just pass in sizeof(str) as the size, and you should get the 9 first letters of your line. Obviously, reading the entire line will severely overflow your 10 character long array, so that would be a bad idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM