searching to a specific line in file

Discussion in 'C++' started by uche, Apr 9, 2007.

  1. uche

    uche Guest

    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');

    }
     
    uche, Apr 9, 2007
    #1
    1. Advertising

  2. 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;
    }
     
    Obnoxious User, Apr 9, 2007
    #2
    1. Advertising

  3. uche wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 9, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,403
    Matt Humphrey
    Oct 18, 2004
  2. fazeee

    searching specific file...?

    fazeee, May 4, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    331
  3. scad
    Replies:
    23
    Views:
    1,222
    Alf P. Steinbach
    May 17, 2009
  4. Psapg
    Replies:
    1
    Views:
    84
  5. stumblng.tumblr
    Replies:
    1
    Views:
    238
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page