Thread: Reading files(very basic)

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Red face Reading files(very basic)

    How can I read a string(I mean the string class) from a file?
    What a shame, such ridiculous question. I tried to find it in my BC++ reference, didnīt find anything...
    Thanks any answer!
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
                ifstream fin("file.txt");
                string s;
                fin >> s;
                return 0;
    }
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Talking

    Thanks!
    I was doing

    s << fin;

    But one more question, does this read trouhg blank spaces?
    For example: as far as I know, cin << s would read characters until Enter is pressed, but would "cut" at the first blank space...
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    That method stops at the first space. To get a whole line of input you'll need to use getline.
    Code:
    getline(fin, str);
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Talking

    Most perfect!
    Thanks!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with a basic prog (reading input files)
    By BoLoB in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2007, 03:35 PM
  2. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM
  3. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  4. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 05:49 PM
  5. reading float from file
    By arjunajay in forum C++ Programming
    Replies: 10
    Last Post: 07-30-2005, 11:01 PM