File Line Handling

Hi, I am doing some simple file io with filestreams.  Currently I am reading line by line via getline and parsing the line read.  Is there a way that I can choose to jump to a particular line in the file and read it?  The file I am working with is huge (10000+ lines) and it takes a while to call getline on every line in the file.  If I know the line I want to read is there an easier way around this?  Thanks.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

i would read the whole file into memory and then you can just wiz through it in a loop counting the new lines. it would be impossible to jump to a new without reading up to that line..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If the no. of bytes in a line are fixed and u are aware of that number, then u can use seekg () to position the get pointer to the specific line

If the number of bytes in each line is variable, then you can create an index table that points to the starting position in the file for each line.
Then when you need a specific line, just look it up in the index table, and do a seekg() to get to the position in the file.

Another alternative is to use map view api functions, that maps the entire file contents to memory.
If you can afford to preload the file into a vector of strings, you'll have something quite easy to work with.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main(int argc,const char *argv[])
      if (argc != 2) return (std::cerr << "Usage: " << argv[0] << " {filename}\n"),1;
      typedef std::vector<std::string> LineList;
      LineList lineList;
      lineList.reserve(10000);      // Reserve some space
            std::ifstream fin(argv[1]);
            if (!fin) return (std::cerr << "Error: " << argv[1] << " cannot be read\n"),1;
            std::string line;
            while (getline(fin,line))

      for (;;) {
            std::cout << "Choose a line number (1-" << lineList.size() << "): ";
            int line;
            if (!(std::cin >> line))
            if (line < 1 || line > lineList.size())
            std::cout << lineList[line-1] << '\n';
      return 0;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.