?
Solved

getline

Posted on 2007-11-27
13
Medium Priority
?
398 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 960 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 1040 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Assisted Solution

by:lucky_james
lucky_james earned 960 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 1040 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 960 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 960 total points
ID: 20357659
in above loop.....
Nth line would be in Nth array element.

0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1040 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 960 total points
ID: 20357813
>>i got this error message

what you got as error message??
0
 
LVL 53

Assisted Solution

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

Author Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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