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;
} 8 6031
nick wrote: 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
nick wrote: 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
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.
nick wrote: 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
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.
nick wrote: 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()
nick wrote: 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.
nick wrote: Please quote what you are replying to
See below.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jerry |
last post by:
Hi people:
Can somebody show me a quick code snippet to reliably extract an
email-address form a text file ?
Any help greatly appreciated!
Jerry
|
by: steveyjg |
last post by:
I want to extract the following data from a retrieved html file and store the information as strings.
'get the text of "title"
<h1 id="test_title">title</h1>
'get the contents of the value...
|
by: napolpie |
last post by:
DISCUSSION IN
USER nappie writes:
Hello, I'm Peter and I'm new in python codying and I'm using parsying
to extract data from one meteo Arpege file. This file is long file and
it's composed by...
|
by: maylee21 |
last post by:
hi, anyone can help me figure out how to read data from a text file
like this:
10980012907200228082002
and extract the data according to this kind of format:
Record type 1
TY-RECORD ...
|
by: suis |
last post by:
Hi Everybody,
I have a big dought about, how to read meta data information in a specific file type like .MSG , anyway thanks to this URL bellow . Edanmo has done a great job for that. ...
| |
by: learningvbnet |
last post by:
Hi,
I am trying to extract zipped files using Winzip in my VB.net application and I ran into 2 stone walls.
1. How do you handle file names with spaces. See psiProcess.Arguments
For...
|
by: veer |
last post by:
Hi
i am making a program in which i want to extract data from html file .
Actually there are two dates on html file i want to extract these dates but the main probleum is that these dates are...
|
by: Dennis |
last post by:
Hi, I have a text file that contents a list of email addresses like
this:
"foo@yahoo.com"
"tom@hotmail.com"
"jerry@gmail.com"
"tommy@apple.com"
I like to
|
by: JoeC |
last post by:
I am trying to create a windows program that reads binary graphics as
a resource. This has nothing to do with win32 but conversion of data
with memcpy.
graphic::graphic(UINT uiResID, HINSTANCE...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |