Solved

getline

Posted on 2007-11-27
13
394 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

803 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