File Line Handling

Posted on 2004-11-17
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

    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

    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

    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

    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;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    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.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now