-
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.
-
The Tut section or Google might give you some nice informations about File IO.
-
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/
-
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."
-
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.
-
Sure! You can open multiple files, read from them, read from one and write to another, etc.
-