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

x
?
Solved

File Line Handling

Posted on 2004-11-17
4
Medium Priority
?
212 Views
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.
0
Comment
Question by:ChronicRAGE
4 Comments
 
LVL 10

Accepted Solution

by:
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..
0
 
LVL 10

Expert Comment

by:Sys_Prog
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

Amit
0
 
LVL 30

Expert Comment

by:Axter
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.
0
 
LVL 17

Assisted Solution

by:rstaveley
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.

--------8<--------
#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))
                  lineList.push_back(line);
      }

      for (;;) {
            std::cout << "Choose a line number (1-" << lineList.size() << "): ";
            int line;
            if (!(std::cin >> line))
                  break;
            if (line < 1 || line > lineList.size())
                  break;
            std::cout << lineList[line-1] << '\n';
      }
      return 0;
}
--------8<--------
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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