Solved

Getting specific lines in an input file.

Posted on 2007-11-29
7
209 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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 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.

810 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