Hello,
I'm trying to create a C++ command line program that will extract specific
lines from a text file and write them to another text file. E.g. If I want
the program to look for the word "The"(Always at the start) and extract the
entire line.
Text file input example:
// ------------------------
Hello there!
The quick brown fox
Jumped over the
lazy dog
The pizza arrived
on time
// ------------------------
Would output
The quick brown fox
The pizza arrived
I know this can be done with VB using strings and InStr. I've found that
strings can be examined using
string.find(word to find);
However, when reading data from a file, I don't seem to be able to use a
string to put the data read from a file into.
I've tried using
// -------
ifstream filein;
filein >> variable;
// And
filein.read(variable, character-limit);
// -------
Neither appears to accept using a string variable as a variable for that, I
can get it to work as a char, double, etc; but when I use char I stops at
spaces and ignores the ends of lines.
Thank you,
Andy.
Tamas Demjen - 08 Feb 2007 22:44 GMT
> However, when reading data from a file, I don't seem to be able to use a
> string to put the data read from a file into.
Take a look at getline:
std::ifstream is;
std::string str;
std::getline(is, str, '\n');
http://msdn2.microsoft.com/ru-ru/library/3ah895zy(VS.80).aspx
Tom
Andy - 08 Feb 2007 23:28 GMT
> > However, when reading data from a file, I don't seem to be able to use a
> > string to put the data read from a file into.
[quoted text clipped - 7 lines]
>
> Tom
This worked perfectly, thank you very much. =)
- Andy