?
Solved

getline

Posted on 2007-11-27
13
Medium Priority
?
399 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

649 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