getline

std::getline() returns

I have a loop that reads each line in a file using getline(). istream is; string line; while (!getline(is, line).eof()) { } I noticed that calling getline() like this also seems to work: while (getline(is, line)) What's going on here? getline() returns a stream reference. Is it being converted to a pointer somehow? Is this actually...

getline() in C++ - _GNU_SOURCE not needed?

Firstly, I'm pretty new to C++. I believe that getline() isn't a standard C function, so #define _GNU_SOURCE is required to use it. I'm now using C++ and g++ tells me that _GNU_SOURCE is already defined: $ g++ -Wall -Werror parser.cpp parser.cpp:1:1: error: "_GNU_SOURCE" redefined <command-line>: error: this is the location of the previ...

Why is stringstreams rdbuf() and str() giving me different output?

I have this code, int main() { std::string st; std::stringstream ss; ss<<"hej hej med dig"<<std::endl; std::getline(ss,st,' '); std::cout <<"ss.rdbuf()->str() : " << ss.rdbuf()->str(); std::cout <<"ss.rdbuf() : " << ss.rdbuf(); return 0; } Giving me this output ss.rdbuf()->str() : hej hej med dig ...

Are there alternate implementations of GNU getline interface?

The experiment I am currently working uses a software base with a complicated source history and no well defined license. It would be a considerable amount of work to rationalize things and release under a fixed license. It is also intended to run a a random unixish platform, and only some of the libc's we support have GNU getline, but ...

is it possible to have getline() function accept wistream&

Just for clarification, I'm referring to the global getline() function in the string class. What I want to do is to have something like this: int main() { wifstream in(L"textfile.txt"); someFunc(in); return 0; } void someFunc(const wistream& read) { wstring buff; while(getline(read, buff)) { //do some proces...

Need help with getline()

Is there a reason why if in my program I am asking the user for input, and I do: int number; string str; int accountNumber; cout << "Enter number:"; cin >> number; cout << "Enter name:"; getline(cin, str); cout << "Enter account number:"; cin >> accountNumber; Why after inputting the first number, it outputs "Enter Name", followed im...

Getline reading in a csv very oddly

I am trying to read a csv using get line to extract three variables separated by commas. Name, Course, and Grade. I am reading in the first line fine but it puts in weird new line breaks and sends the format into a cluster. Here is my code : #include "header.h" string student::GetCourse() { return course; } string student::GetNa...

[C++] std::getline does not work inside a for-loop

Hello there, I'm trying to collect user's input in a string variable that accepts whitespaces for a specified amount of time. Since the usual cin >> str doesn't accept whitespaces, so I'd go with std::getline from <string> Here is my code: #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace st...

Segmentation fault depending on string length?

I am writing a program that will read lines from an infile using getline into strings, convert the strings to c-strings containing the first m nonwhitespace characters of the string, then concatenate the c-strings into a single char array. A sample file might look something like this: 5 //number of rows and columns in a grid 2 //...

How to use getline in AWK with a command that is concatenated together

This is driving me insane. All I want to do is pass a command to the terminal from awk, where the command is a string concatenated together made from other variables. The documentation for awk says that something like "echo" $1 | getline var should put the value of $1 into var. But this is not the case. What am I missing here? I sh...

C++ using getline() prints: pointer being freed was not allocated in XCode

I'm trying to use std:getline() but getting a strange runtime error: malloc: * error for object 0x10000a720: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug This is the code that produces this error: //main.cpp #include <iostream> #include <sstream> int main (int argc, char * const argv[]...

checking for eof in string::getline

Hi, how do I check for end-of-file using the string::getline function? Coz, using .eof() is not recommended as it wont signal eof until I attempt to read beyond eof. ...

gawk / awk: piping date to getline *sometimes* won't work

Hi! I'm attempting to convert dates from one format to another: From e.g. "October 29, 2005" to 2005-10-29. I have a list of 625 dates. I use Awk. The conversion works -- most of the time. Hovewer, sometimes the conversion won't happen at all, and the variable supposed to hold the (converted) date remains undefined. This always happen...

C++: getline() ignoring first several characters

In my program, fin is an ifstream object and song is a string. When the program runs, it opens music.txt and reads from the file. I try to read each line with: getline(fin,song); I've tried all variations of getline but it keep ignoring the first 10 or so characters of each line before it starts picking up characters. For instance, if ...

C++ help with getline function with ifstream

So I am writing a program that deals with reading in and writing out to a file. I use the getline() function because some of the lines in the text file may contain multiple elements. I've never had a problem with getline until now. Here's what I got. The text file looks like this: John Smith // Client name 123...

getline(cin, variable) not wanting to work properly in c++?

Here's my program so far: int main() { char choice = 'D'; string inputString; cout << "Please input a string." << endl; getline(cin, inputString); LetterCount letterCount(inputString); while(choice != 'E') { cout << "Please choose from the following: " << endl << "A) Count the number o...

C++ How do I properly use getline for ifstream members.

Ok so I have a problem with getline. I have a file that contains a couple strings. I created it by myself and I have each string on a seperate line. Ex. textfile.txt Line 1 Line 2 Line 3 Line 4 //Little snip of code ifstream inFile("textfile.txt"); getline(inFile, string1); When I debug the program and ask it...

getline() returns empty line in Eclipse but working properly in Dev C++

Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int main() { string line; ifstream inputFile; inputFile.open("input.txt"); do { getline(inputFile, line); cout << line << endl; } while (line != "0"); return 0; } input.txt content: 5 9 2 9 3 8 ...

Ways std::stringstream can set fail/bad bit?

A common piece of code I use for simple string splitting looks like this: inline std::vector<std::string> split(const std::string &s, char delim) { std::vector<std::string> elems; std::stringstream ss(s); std::string item; while(std::getline(ss, item, delim)) { elems.push_back(item); } return elems; } S...

is there a way to use cin.getline() without having to define a char array size before hand?

Basically my task is having to sort a bunch of strings of variable length ignoring case. I understand there is a function strcasecmp() that compares cstrings, but doesn't work on strings. Right now I'm using getline() for strings so I can just read in the strings one line at a time. I add these to a vector of strings, then convert to cst...