Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using getline() with file input in C++

Tags:

c++

getline

I am trying to do a simple beginner's task in C++. I have a text file containing the line "John Smith 31". That's it. I want to read in this data using an ifstream variable. But I want to read the name "John Smith" into one string variable, and then the number "31" into a separate int variable.

I tried using the getline function, as follows:

ifstream inFile;
string name;
int age;

inFile.open("file.txt");

getline(inFile, name); 
inFile >> age; 

cout << name << endl;
cout << age << endl;  

inFile.close();    

The problem with this is that it outputs the entire line "John Smith 31". Is there a way I can tell the getline function to stop after it has gotten the name and then kind of "restart" to retrieve the number? Without manipulating the input file, that is?

like image 991
nothingIsMere Avatar asked Dec 23 '13 08:12

nothingIsMere


People also ask

What does getline () do in C?

The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.

How do you get a specific line in a file C?

Solution 1 You can use fgets [^] in a loop to read a file line by line. When no more lines can be read, it will return NULL. On the first line you can use sscanf[^] to extract the integer.

Does Getline work with binary files?

It works in your simple case, and other equally simple cases. It won't work generally. Remember that binary files can contain any bytes in any order, including zeroes (which is what '\0' is) anywhere, especially where you don't expect it.

What does Getline return at end of file?

Returned value If successful, getline() returns the number of characters that are read, including the newline character, but not including the terminating null byte ( '\0' ). This value can be used to handle embedded null bytes in the line read.


3 Answers

getline, as it name states, read a whole line, or at least till a delimiter that can be specified.

So the answer is "no", getlinedoes not match your need.

But you can do something like:

inFile >> first_name >> last_name >> age;
name = first_name + " " + last_name;
like image 109
Johan Avatar answered Sep 19 '22 01:09

Johan


ifstream inFile;
string name, temp;
int age;

inFile.open("file.txt");

getline(inFile, name, ' '); // use ' ' as separator, default is '\n' (newline). Now name is "John".
getline(inFile, temp, ' '); // Now temp is "Smith"
name.append(1,' ');
name += temp;
inFile >> age; 

cout << name << endl;
cout << age << endl;  

inFile.close();    
like image 35
SliceSort Avatar answered Sep 21 '22 01:09

SliceSort


you should do as:

getline(name, sizeofname, '\n');
strtok(name, " ");

This will give you the "joht" in name then to get next token,

temp = strtok(NULL, " ");

temp will get "smith" in it. then you should use string concatination to append the temp at end of name. as:

strcat(name, temp);

(you may also append space first, to obtain a space in between).

like image 33
Zeeshan Avatar answered Sep 19 '22 01:09

Zeeshan