extract data from file

N

nick

Hi all
can any one please tell me what is wrong in this code??
I'm new to deal with text files and extract data.
i'm trying to look for data in a text file (3~4 pages) some lines start

with a word "red" first if find(red) then print the last 5 letters of
that string and if red is not found at the begining of the string then
do nothing and
go to another line.
how can I also do this using find( )??
thanks


#include <iostream>
#include <fstream>
#include <string>

int main( )
{
ifstream textfile ("textfile.txt");

string text;
string::size_type posn;

if (textfile.is_open())
{
while (getline(textfile, text))
{
posn = text.find("red");
if (line.search(red) != std::string:no_pos)
//if (posn == string::npos)
{
continue;
}


std::string data = text.substr(5);
cout<<" " << data << endl;
}
}


else cout << "can't open file" << endl;


system("PAUSE");
return EXIT_SUCCESS;
}
 
V

Victor Bazarov

nick said:
can any one please tell me what is wrong in this code??

Does it compile? If no, what errors do you get? If it does compile, does
it work as intended? Why should we tell you when you probably already
know what is wrong? Now, once you tell us what is not the way you want it
to be, we could try to help you to make it the way it should be. But
without knowing the symptoms, how can anybody tell?
I'm new to deal with text files and extract data.
i'm trying to look for data in a text file (3~4 pages)

Perhaps you should start with a simple set of test data. Like a file that
only contains three lines...

> some lines start

with a word "red" first if find(red) then print the last 5 letters of
that string and if red is not found at the begining of the string then
do nothing and
go to another line.
how can I also do this using find( )??

Aren't you already using 'find'?
thanks


#include <iostream>
#include <fstream>
#include <string>

You need

using namespace std;

here, probably.
int main( )
{
ifstream textfile ("textfile.txt");

string text;
string::size_type posn;

if (textfile.is_open())
{
while (getline(textfile, text))
{
posn = text.find("red");

I think you need to (a) skip whitespace in 'text', then (b) extract the
first three chars of 'text' using the 'substr' member and compare the
extracted string with "red".
if (line.search(red) != std::string:no_pos)

What's "search"? What's "line"? Don't you mean to compare what you
just obtained from 'find'?

Even if you do compare the right things, it doesn't necessarily mean your
"word" is found at all. If I have a word "referred" or "predicate"
somewhere in a line, your 'find' will give you the position of it, but
it won't satisfy the condition you stated (line beginning with the word
"red").
//if (posn == string::npos)
{
continue;
}


std::string data = text.substr(5);
cout<<" " << data << endl;
}
}


else cout << "can't open file" << endl;


system("PAUSE");
return EXIT_SUCCESS;
}

V
 
I

int2str

nick said:
Hi all
can any one please tell me what is wrong in this code??

It doesn't compile for once.
Please post compilable code if you want us to help you with a language
related problem.
I'm new to deal with text files and extract data.
i'm trying to look for data in a text file (3~4 pages) some lines start
with a word "red" first if find(red) then print the last 5 letters of
that string and if red is not found at the begining of the string then
do nothing and
go to another line.
how can I also do this using find( )??

Assuming this is no homework....

If the lines START with the word 'red' and there's no leading
whitespace etc. You don't need to use find. Indeed find() may yield
incorrect results if you have lines like "greenish red 12345".
thanks


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

or

using std::string; // etc.
int main( )
{
ifstream textfile ("textfile.txt");

string text;
string::size_type posn;

posn can be declared closer to where it's used.
if (textfile.is_open())
{
while (getline(textfile, text))
{
posn = text.find("red");
if (line.search(red) != std::string:no_pos)

What's "line", what's "search" and what is "no_pos" ???
Also your condition is the inverse of the one in the comment.
//if (posn == string::npos)
{
continue;
}


std::string data = text.substr(5);

This will give you the string from position 5 in text onwards. As in,
given text is "Hello World", data will be " World". This is not what
you want.
cout<<" " << data << endl;
}
}


else cout << "can't open file" << endl;

If you used curly braces for the if(), use it for the else, too.
Also fix your indenting etc.
system("PAUSE");

Not portable.
return EXIT_SUCCESS;
Unecessary.

}

Cheers,
Andre
 
N

nick

so what is the best way to check the first word in a line?
if match then do some thing and if not then skip to next line.
 
I

int2str

nick said:
so what is the best way to check the first word in a line?
if match then do some thing and if not then skip to next line.

Please quote what you are replying to.

It seems what you have posted originally is "meta code". Why not pick
up a book, or research the actual functions and classes online. Learn
about std::string, how to use substr(), == etc.

Post back what you come up with.

Cheers,
Andre
 
N

nick

Please quote what you are replying to

If the lines START with the word 'red' and there's no leading
whitespace etc. You don't need to use find. Indeed find() may yield
incorrect results if you have lines like "greenish red 12345".

here the word red only exists at the begining of the certain lines.

in general: what is the best way to check the first word in a line?
if match then do some thing and if not then skip to next line.
 
R

red floyd

nick said:
Please quote what you are replying to

If the lines START with the word 'red' and there's no leading
whitespace etc. You don't need to use find. Indeed find() may yield
incorrect results if you have lines like "greenish red 12345".

here the word red only exists at the begining of the certain lines.

in general: what is the best way to check the first word in a line?
if match then do some thing and if not then skip to next line.

Actually, you had a reasonable shell: you're using istream, you're using
std::getline() properly, your not misusing eof().

Hints:
look at std::string::find_first_not_of() to find the first non-whitespace.
Then look at substr()
 
R

red floyd

nick said:
Please quote what you are replying to

If the lines START with the word 'red' and there's no leading
whitespace etc. You don't need to use find. Indeed find() may yield
incorrect results if you have lines like "greenish red 12345".

here the word red only exists at the begining of the certain lines.

in general: what is the best way to check the first word in a line?
if match then do some thing and if not then skip to next line.

Alternatively, look at std::istringstream, for parsing your line of input.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top