hi, one aspect of a program i am creating needs to extract a certain
line of text from a simple ".txt" file. However i can only find a way
of extract everything present in the file using i/o streams. Below is
the code i used to extract the whole text, is there any way of
extracting a certain line or sentance?
string line;
ifstream myfile ("example.txt") ;
if (myfile.is_open ())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
using the header files
#include <iostream>
#include <fstream>
#include <string> 3 1934
"Wilson" <tp****@googlem ail.comwrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
hi, one aspect of a program i am creating needs to extract a certain
line of text from a simple ".txt" file. However i can only find a way
of extract everything present in the file using i/o streams. Below is
the code i used to extract the whole text, is there any way of
extracting a certain line or sentance?
string line;
ifstream myfile ("example.txt") ;
if (myfile.is_open ())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
using the header files
#include <iostream>
#include <fstream>
#include <string>
Unless te file is indexed someway, the only way to know where the line of
text is is by reading the file until you find it.
Incidnetly, you can get other errors rather than end of file, so this loop
is generally prefered:
std::string Line;
while ( std::getline( myfile, line )
{
}
getline will return false if any error condition is hit (including end of
file).
Anyway...
std::string Line;
int LineNumber = 0;
while ( std::getline( myfile, Line ) && ++Line != 10 );
if ( Line == 10 )
std::cout << "Line 10:" << Line << "\n";
else
std::cout << "File too short or error occured\n";
myfile.close();
Comment: myfile.close() is not required. When the desturctor for the
ifstream is called the file will be closed automatically. :But I also
manually close the file myself.
You may also notice this is an empty while statement. If it makes you feel
better you can write it like this:
while ( std::getline( myfile, Line ) && ++Line != 10 ) {};
or
while ( std::getline( myfile, Line ) && ++Line != 10 )
{
};
But they all do the same thing, just read a line until an error occurs or
line 10 is read.
On Fri, 9 Feb 2007 10:13:39 -0800, "Jim Langston" wrote:
std::string Line;
while ( std::getline( myfile, line )
{
}
getline will return false if any error condition is hit (including end of file).
That getline will return false is imprecise at least since it returns
no bool.
Best regards,
Roland Pibinger
In article <45************ *@news.utanet.a t>, rp*****@yahoo.c om says...
On Fri, 9 Feb 2007 10:13:39 -0800, "Jim Langston" wrote:
std::string Line;
while ( std::getline( myfile, line )
{
}
getline will return false if any error condition is hit (including end of
file).
That getline will return false is imprecise at least since it returns
no bool.
std::getline returns a reference to the stream being read, which can be
converted to a bool indicating whether there has been an error on the
stream.
--
Later,
Jerry.
The universe is a figment of its own imagination. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ferran |
last post by:
Hi, I'm trying to convert a string to a long double using streams but
for some reasing seems to give the wrong values, the idea is to do a
precise textual conversion without roundings or truncations, I though
about using strtold but this function is not portable hence that is
why I'm using streams, this is what I'm doing
//-------------------------------------------------------
std::stringstream oStream;
std::string sInput =...
|
by: Tron Thomas |
last post by:
What does binary mode for an ofstream object do anyway? Despite which
mode the stream uses, operator << writes numeric value as their ASCII
representation.
I read on the Internet that it is possible to change the behavior of
operator << so it will stream numeric values as their actual values
when an ofstream is in binary mode. I did not, however, find any
information on how this can be accomplished. What is involved in
getting this...
|
by: bonj |
last post by:
hello
I hope somebody can help me get my head around this area of 'stream'
programming... I know that streams are very fashionable nowadays so
hopefully there'll be lots of replies. ;-)
Basically I have an operation which the input and output for are streams -
a function which receives a certain 'chunk' of data each time it runs, and
it runs many times until it has received all the data, in a similar way to
a socket.
What the...
|
by: radnoraj |
last post by:
Hi,
I am sucessfull in redirecting console output to a file. but in this
case nothing is displayed on the console, cout output is written to
file without display.
how do write the output to console as well as to file, my code is as
below,
=======================================================================
#include <iostream.h>
#include<ostream>
#include<sstream>
|
by: Kobu |
last post by:
I have a question about C's abstract "streams" (that I can't seem to
FULLY understand from reading several tutorials).
Streams seems to suggest that input can be treated continously if
needed. Is this true?
So far as I know, I can only get a stream to "flow" when there is a
'\r'. At that point the '\r' is turned into a '\n' and the whole
stream is sent to some abstract area that is now only accessible to my
programs (meanwhile, other...
| |
by: Bonj |
last post by:
I downloaded the gzlib library from zlib in order to do compression.
(http://www.gzip.org/zlib)
The prototype of the compression function seems to be
int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong
sourceLen);
It is meant to be called by C, but I would rather use it from C#.
So I wrote the following C# program to test it, but it failed to work. The
call to compress doesn't return or throw an exception, it simply...
|
by: bonk |
last post by:
Hello
how do I connect streams in c# ?
Imagine the followung scenario: I have a StreamWriter that writes Text to a
Stream. How can I tell that Stream to pass that Data to another Stream
(connect it to another output Stream) ? In java I am used to connect
streams to each other via thier Constructors.
For example:
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("soc.txt"),
|
by: Just Me |
last post by:
Using streams how do I write and then read a set of variables?
For example, suppose I want to write into a text file:
string1,string2,string3
Then read them later.
Suppose I want to write and then read:
string1, integer1, double1
|
by: Abhishek |
last post by:
what are the STDUPDATE, STDERR, STDOUT and STDIN streams and how does
one access these streams in C language. I am aware of the function
fprintf(FILE *fp, char * format, char *s) which puts the string into
the corresponding streams. But can you please tellme where does the
content go exactly when we put it into the above streams. In which
cases can we see the outpt and in which cases cant we see and why so?
It would be great if you can...
|
by: Christian Heimes |
last post by:
Grant Edwards wrote:
It's dangerous to mix streams and descriptors. Traditionally freopen()
is used to redirect a standard stream to a file:
http://www.gnu.org/software/libc/manual/html_mono/libc.html#Standard-Streams
— Function: FILE * freopen (const char *filename, const char *opentype,
FILE *stream)
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |