Solved

getline

Posted on 2007-11-27
13
392 Views
Last Modified: 2010-04-01
I have a txt file with 4 lines


line1
line2
line3
line4



I want to use a loop to store each line into an element in an array

string line[10];
int n = 0;
while ( n < 5 )
{
  //  getline
  n = n + 1;
}

return 0;



what is the syntax to read whole N th line in the file?
0
Comment
Question by:Troudeloup
  • 5
  • 4
  • 4
13 Comments
 
LVL 7

Assisted Solution

by:lucky_james
lucky_james earned 240 total points
ID: 20357545
you can use a for loop and inside the loop use the following statement :
cin.getline (arr[loop_counter],256);



More about getline:
http://www.cplusplus.com/reference/iostream/istream/getline.html

Let me know if you have any doubts.
0
 

Author Comment

by:Troudeloup
ID: 20357592
i should have said this:

I want to read from a txt file.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 260 total points
ID: 20357626
Something like this would be nicer :

vector<string> lines;

string line;

ifstream infile("the_file_name");

while (getline(infile, line)) {

    lines.push_back(line);

}

infile.close();

Open in new window

0
 
LVL 7

Assisted Solution

by:lucky_james
lucky_james earned 240 total points
ID: 20357630
not a problem.......use file i/o instead of normal i/o operations

check out:
http://www.mkssoftware.com/docs/man3/fgetln.3.asp

Hope it helps.
0
 

Author Comment

by:Troudeloup
ID: 20357642
while (getline(infile, line)) {
    lines.push_back(line);
}




i don't get this part.



ok how about, can you show me how to get the N th line and ignore the loop for now?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 260 total points
ID: 20357644
And if you want to keep your array, then you can do something like this :

string lines[MAX];

int i = 0;

ifstream infile("the_file_name");

while ((i < MAX) && getline(infile, lines[i++]));

infile.close();

Open in new window

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

 
LVL 7

Accepted Solution

by:
lucky_james earned 240 total points
ID: 20357648
Continuiing from my comment above :

string line;
ifstream filePtr("file_name");
for (Count = 0 ; getline(filePtr, line); i++)
{
    arr[Count] = line;
}


Hope it helps.
0
 
LVL 7

Assisted Solution

by:lucky_james
lucky_james earned 240 total points
ID: 20357659
in above loop.....
Nth line would be in Nth array element.

0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 260 total points
ID: 20357663
>> i don't get this part.

What don't you get ? getline gets the next line from the file, and stores it in the string line, and the push_back adds that line to the vector :

        http://www.cplusplus.com/reference/string/getline.html
        http://www.cplusplus.com/reference/stl/vector/push_back.html


>> ok how about, can you show me how to get the N th line and ignore the loop for now?

To read the n-th line, you'll still need a loop to skip the first (n - 1) lines, and then read the n-th line.
0
 

Author Comment

by:Troudeloup
ID: 20357771
#include <iostream>
#include <windows.h>


using namespace std;




int main ()
{

      string line;
      ifstream filePtr("lines.txt");
      getline(filePtr, line);
      cout << line << endl;
      
      
      return 0;
}



i got this error message

0
 
LVL 7

Assisted Solution

by:lucky_james
lucky_james earned 240 total points
ID: 20357813
>>i got this error message

what you got as error message??
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 260 total points
ID: 20357814
#include <fstream>
#include <string>
0
 

Author Comment

by:Troudeloup
ID: 20357824
ah duh  >_<
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

747 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

13 Experts available now in Live!

Get 1:1 Help Now