Re: how to match a particular string in a input file
On Apr 12, 5:04 am, Pradeep <bubunia2000s...@gmail.com> wrote:
I have a file a log.txt. I have read the file each line with
the following.
ifstream inputFile("log.txt");
std::string line;
if (inputFile.is_open())
{
while (! inputFile.eof() )
This will probably cause you to skip the last line.
Where do people "learn" this sort of thing? (It's a frequent
error, so somewhere, it must be being taught.) The standard
idiom for reading lines from a stream is either:
std::string line ;
while ( std::getline( inputFile, line ) ) ...
or to define a class Line, with an overloaded operator<<, use:
Line line ;
while ( inputFile >> line ) ...
(For most types, the class is the way to go, but in the case of
lines, it's less certain---usually, a "line" doesn't correspond
to anything in the application; it's just a convenient way to
parse the file, with resynchronization in case of error.)
{
getline (inputFile,line,"\n");
// cout << line << endl;
}
inputFile.close();
}
log.txt is of following form
------------------------------------
Source File: m.log
File Size: 45333 bytes
Start Time: Feb 20 2000
Comment:
Dest. File: dest.log
m.log is already in prescribed file format.
Which is? <label>:<data>, with the first colon
as separator? Or something else? (Does what follows the colon
have some specific format, for example.)
My Question is If I want to read a partial message "already in
a prescribed format" in a line how do I do that?
What do you mean by a "partial message"?
Splitting strings at the first colon is trivial:
std::string line ;
// ...
std::string::const_iterator
pivot
= std::find( line.begin(), line.end(), ':' ) ;
if ( pivot == line.end() ) {
// error, no colon found...
} else {
std::string label( line.begin(), pivot ) ;
std::string data( pivot + 1, line.end() ) ;
// ...
}
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34