[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


File Line Handling

Posted on 2004-11-17
Medium Priority
Last Modified: 2010-04-01
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.
Question by:ChronicRAGE
LVL 10

Accepted Solution

makerp earned 120 total points
ID: 12612218
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..
LVL 10

Expert Comment

ID: 12612451
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

LVL 30

Expert Comment

ID: 12612661
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.
LVL 17

Assisted Solution

rstaveley earned 80 total points
ID: 12613019
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;

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question