Solved

getline

Posted on 2007-11-27
13
397 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
[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
  • 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
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!

 
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
 
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

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

696 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