Solved

Getting specific lines in an input file.

Posted on 2007-11-29
7
213 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
Independent Software Vendors: 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 350 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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 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