Using i/o streams

W

Wilson

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>
 
J

Jim Langston

Wilson said:
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.
 
R

Roland Pibinger

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
 
J

Jerry Coffin

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.
 

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

Text File I/O 2
file open in C++ 15
Rearranging .ply file via C++ String Parsing 0
fstream File i/o 1
File I/O and conversion 10
help with file I/O 4
safely reading large files 10
C++ input from file processing 3

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top