Thread: C++ and txt files

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    C++ and txt files

    Hey guys, I am new to c++ and I can now use c++ to open and read txt files but I was wondering is it possible do to other things with the files such as pick out only certain words to be displayed from the file? Also I was wondering where I could find out more information about what else you can do with c++ and txt files?

    Thanks for any advice.

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    The Tut section or Google might give you some nice informations about File IO.

  3. #3
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Load everything in the text file into a string, then use find() function to find and pickout words you liked.
    Link to example of find() function:
    http://www.cppreference.com/cppstring/
    Hello, testing testing. Everthing is running perfectly...for now

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    A good beginner's example to use when trying to get info from the text file is the getline() or simply the infile commands.

    Since you know how to open the text file
    e.g ifstream infile; infile.open("blah.txt");

    You can use the infile command to get info from the textfile
    Say the infile looks something like this:

    This is the infile.

    You can say
    string this;
    infile>>this;
    cout<<this;

    Prints "This"

    or

    getline(infile, this);
    cout<<this;

    Prints "This is the infile."

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for all the replies, as you can pick out certain words from a file is it possible to then say use other files as well and compare them or can only one file be used at a time?

    Thanks.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Sure! You can open multiple files, read from them, read from one and write to another, etc.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks

Popular pages Recent additions subscribe to a feed