Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting specific lines in an input file.

Posted on 2007-11-29
7
Medium Priority
?
217 Views
Last Modified: 2010-04-01
Ok my question is how would I grab certain lines in a input file? lets say the input file has these sentences:

input.txt

Showing line 1
Showing line 2
showing line 3
etc...

now how would I get a specific line?

I know how to get first line by using

getline(file,str) //assuming string str; and ifstream file;
0
Comment
Question by:jschmuff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20377625
If the lines don't have a fixed length, then you'll have to read one line at a time until you reach the line you need (discarding all lines you read, except the last).

If the linesDO have a fixed length, you can set the file pointer to the correct location, and then read the line.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20377719
For a general approach, you can

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

ifstream is("input.txt");

vector<string> vLines;

while(!is.is_eof()){

  string sLine;

  getline(is,sLine);

  vLines.push_back(sLine);
}

// show 3rd line
cout << vLines[2] << eof; // 2 because indices are zero-based
0
 

Author Comment

by:jschmuff
ID: 20377826
so there is no way set a spot to start after?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jschmuff
ID: 20377864
jkr>> what is the bracket for after vLine.push_back(sline);
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1400 total points
ID: 20377866
>> so there is no way set a spot to start after?

You can save a position in a file that you can go back to later by using tellg and seekg :

        http://www.cplusplus.com/reference/iostream/istream/tellg.html
        http://www.cplusplus.com/reference/iostream/istream/seekg.html

but of course you'd still have to get to the position you want first before you can use tellg.
0
 

Author Comment

by:jschmuff
ID: 20377867
Oh haha never mind I didnt see the beginning bracket for the while
0
 
LVL 86

Expert Comment

by:jkr
ID: 20377877
>>what is the bracket for after vLine.push_back(sline);

Um, what bracket? The one that ends the 'while' loop?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

670 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