searching to a specific line in file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • uche

    #1

    searching to a specific line in file

    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.pe ek() != EOF)
    {

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

    for(int i = 0 ; i < endofRec; i++)
    {
    istrm1.getline( line,512);
    cout<<line<<end l;
    }
    istrm1.seekg(io s::clear,ios::b egin);
    istrm1.clear();
    break;
    }
    istrm1.ignore(1 28, '\n');

    }

  • Victor Bazarov

    #2
    Re: searching to a specific line in file

    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.pe ek() != 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::g etline',
    you'd be able to take advantage of 'std::string's ability to grow
    and will not be limited by 512 characters.
    cout<<line<<end l;
    }
    istrm1.seekg(io s::clear,ios::b egin);
    istrm1.clear();
    break;
    }
    istrm1.ignore(1 28, '\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


    Comment

    • Obnoxious User

      #3
      Re: searching to a specific line in file

      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.pe ek() != EOF)
      {
      >
      ++count;
      if(count==RBNx)
      What is 'RBNx'?
      {
      >
      for(int i = 0 ; i < endofRec; i++)
      {
      istrm1.getline( line,512);
      cout<<line<<end l;
      }
      istrm1.seekg(io s::clear,ios::b egin);
      istrm1.clear();
      break;
      }
      istrm1.ignore(1 28, '\n');
      >
      }
      >
      Your posted code is rather incomplete.

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

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

      void fout(std::istre am & in) {
      std::copy(std:: istream_iterato r<char>(in),
      std::istream_it erator<char>(),
      std::ostream_it erator<char>(st d::cout));
      }

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

      Comment

      Working...