how to match a particular string in a input file

P

Pradeep

Hi all,
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() )
{
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.


My Question is If I want to read a partial message "already in a
prescribed format" in a line how do I do that?


Regards
Pradeep
 
V

Victor Bazarov

Pradeep said:
Hi all,
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() )
{
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.


My Question is If I want to read a partial message "already in a
prescribed format" in a line how do I do that?

Are you asking us to explain to you how to use 'std::strstr' or the
member 'find' from 'std::string'?

V
 
J

James Kanze

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() ) ;
// ...
}
 
S

smilesonisamal

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.)


Which is?  <label>:<data>, with the first colon
as separator?  Or something else?  (Does what follows the colon
have some specific format, for example.)


What do you mean by a "partial message"?

Thanks for your help.
Partial Message means I want to search in the file *already in
prescribed file format* string of the last line not the full line.
I think I can use the find for that string (instead of ':') right??)
 
Joined
Mar 27, 2009
Messages
8
Reaction score
0
On Apr 13, 2:54*pm, James Kanze <[email protected]> wrote:
> On Apr 12, 5:04 am, Pradeep <[email protected]> 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"?


Thanks for your help.
Partial Message means I want to search in the file *already in
prescribed file format* string of the last line not the full line.
I think I can use the find for that string (instead of ':') right??)
I guess you'd want to use the "find" string member function?
"""
size_t find ( const string& str, size_t pos = 0 ) const;
Searches the string for the content specified in str, and returns the position of the first occurrence in the string.

When pos is specified the search only includes characters on or after position pos, ignoring any possible occurrences in previous locations.
"""

>
> 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:[email protected]
> Conseils en informatique orientée objet/
> * * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
J

James Kanze

On Apr 13, 2:54 pm, James Kanze <[email protected]> wrote:

[...]
Thanks for your help.
Partial Message means I want to search in the file *already in
prescribed file format* string of the last line not the full
line.

I still don't understand what you mean by "string of the last
line not the full line." It sounds like you're looking for a
last line which isn't correctly terminated. (The file doesn't
end with a '\n'.) But somehow, I doubt it.

Or did you mean for "already in prescribed file format" to be in
quotes, rather than in *...*. That that is the string you're
looking for. If so, there is a function search in the standard
library which might help.

.. I think I can use the find for that string (instead of
':') right??)

Probably. String has a number of specialized functions for that
sort of thing. I generally prefer the standard algorithms,
because they will work on any container, but it's possible that
because they are specialized for one particular container, that
the member functions of std::string are slightly faster. (Of
course, if performance is a problem, neither will be very good;
for a high speed search, you need a separate representation,
maintained between calls to the search/find function.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top