Solved

Getting specific lines in an input file.

Posted on 2007-11-29
7
207 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

708 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

20 Experts available now in Live!

Get 1:1 Help Now