Thread: ifstream a string to the screen?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    ifstream a string to the screen?

    Hi,

    I'm trying to input entire lines of text from an open file to the screen. So far, I've only been able to print one word at a time followed by an unwanted newline. What's the secret? And, what recommendations can you give for a text that goes beyond the beginner material?

    Here are some snips:

    ifstream fin("dbase_a.txt", ios::in);
    while (fin >> line))
    {
    outputLine(line);
    }

    void outputLine(const string line)
    {
    cout << line << endl;
    }

    Thanks,
    David Korb

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    4
    Allow me to follow this up by saying "Oops!". Okay, so my unwanted newline was right there in my code, but how do I reproduce the newline that is inherent in the file itself? It all runs together at this point:

    Smith,John:1208:Jan.31,1998Decker,Jane:8012ec.01,1997Mo,Joe:1028:Jan.12,198Rock,Dan:2081:June 01,1998Cahn,Debbie:2801:July16,1997Dusty,Tom:3012: July19,1998Harvey,Sheila:3141:Aug.021998Press any key to continue . . .

    This database was just something to work with. The file I really want to print out is going to be a normal text document.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    You can use getline with an appropriate delimeter. The default is '\n' so it should work with what you need. The reason it doesn't work with operator>> of ifstream is that operator>> uses whitespace as a delimeter so you only get one word at a time.
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        // ios::in is the default, so no need to specify it
        ifstream f("dbase_a.txt");
        string line;
        
        // getline eats the delimeter ('\n' by default) but doesn't
        // store it, so you'll have to consider that when using it.
        while(getline(f, line)) { 
            cout << line << endl;
        }
    }
    Hope this helps.
    - lmov

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Very nice use of comments and instruction, IMOV.

    We need more people like you.
    Blue

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    Cool

    Or you may use the get() function in the following way:


    void main()
    {
    ifstream fin("dbase_a.txt");
    char line; //change line to char type

    while (fin.get(line)) //use get() function
    {
    outputLine(line);
    }
    }

    void outputLine(const char line)
    {
    cout << line; //drop the endl escape-sequence
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM