searching to a specific line in file

U

uche

Hello all!!

Is there an algorithm out there that will allow you to go to a
specific line in a .txt...and start outputing to the screen at that
line until end of record

Here is a code that i implemented...
while(istrm1.peek() != EOF)
{

++count;
if(count==RBNx)
{

for(int i = 0 ; i < endofRec; i++)
{
istrm1.getline(line,512);
cout<<line<<endl;
}
istrm1.seekg(ios::clear,ios::begin);
istrm1.clear();
break;
}
istrm1.ignore(128, '\n');

}
 
O

Obnoxious User

uche skrev:
Hello all!!

Is there an algorithm out there that will allow you to go to a
specific line in a .txt...and start outputing to the screen at that
line until end of record

End of file?
Here is a code that i implemented...
while(istrm1.peek() != EOF)
{

++count;
if(count==RBNx)

What is 'RBNx'?
{

for(int i = 0 ; i < endofRec; i++)
{
istrm1.getline(line,512);
cout<<line<<endl;
}
istrm1.seekg(ios::clear,ios::begin);
istrm1.clear();
break;
}
istrm1.ignore(128, '\n');

}

Your posted code is rather incomplete.

#include <fstream>
#include <string>
#include <ostream>
#include <algorithm>

void fskip(std::istream & in, unsigned nolines) {
std::string rbuf;
while(nolines-- && std::getline(in,rbuf).good());
}

void fout(std::istream & in) {
std::copy(std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::eek:stream_iterator<char>(std::cout));
}

int main(int argc, char* argv[])
{
std::ifstream in("text");
fskip(in,3);
fout(in);
return 0;
}
 
V

Victor Bazarov

uche said:
Is there an algorithm out there that will allow you to go to a
specific line in a .txt...and start outputing to the screen at that
line until end of record

Here is a code that i implemented...
while(istrm1.peek() != EOF)
{

++count;
if(count==RBNx)
{

for(int i = 0 ; i < endofRec; i++)
{
istrm1.getline(line,512);

I think if you use 'std::getline' instead of 'std::stream::getline',
you'd be able to take advantage of 'std::string's ability to grow
and will not be limited by 512 characters.
cout<<line<<endl;
}
istrm1.seekg(ios::clear,ios::begin);
istrm1.clear();
break;
}
istrm1.ignore(128, '\n');

You're only ignoring 128 chars at a time. What if a line is longer?
You'd count it as two lines, right?

V
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top