getline

How does std::getline decides to skip last empty line?

谁都会走 提交于 2019-12-10 20:35:24
问题 I noticed some strange behaviour when reading a file by line. If the file ends with \n (empty line), it may be skipped...but not always, and I don't see what makes it be skipped or not. I wrote this little function splitting a string into lines to reproduce the issue easily: std::vector<std::string> SplitLines( const std::string& inputStr ) { std::vector<std::string> lines; std::stringstream str; str << inputStr; std::string sContent; while ( std::getline( str, sContent ) ) { lines.push_back(

Read a very long console input in C++

☆樱花仙子☆ 提交于 2019-12-10 16:48:47
问题 I'm trying to read a list of numbers (space delimited) from the console using std::cin. When the input line is longer than 1023 characters, the first "cin >> list[i]" in the following small working example never returns: using namespace std; int main() { vector<int> list(200,0); for(int i=0;i<200;i++){ cin >> list[i]; cout << '-'; cin.clear(); if(list[i]==0) break; } return 0; } This code fails for the following input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

getline skipping first input character c++ [closed]

社会主义新天地 提交于 2019-12-10 16:17:55
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . So I have been making this program for a while now. I have looked all over the internet and none of the solutions I have found work. Every time I put in

No `while (!my_ifstream.eof()) { getline(my_ifstream, line) }` in C++?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 15:39:02
问题 On this website, someone writes: while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } This is wrong, read carefully the documentation for the eof() memberfunction. The correct code is this: while( getline( myfile, line)) cout << line << endl; Why is this? 回答1: There are two primary reasons. @Etienne has pointed out one: reading could fail for some reason other than reaching the end of the file, in which case your first version will go into an infinite loop. Even with no

codeblocks and C undefined reference to getline

[亡魂溺海] 提交于 2019-12-10 12:08:24
问题 I am trying to use getline in C with codeblocks and I am having trouble getting it to run on my machine. The code works on a server I have access too but I have limited wifi access so I need this to work on my machine. I am running windows 8.1 64 bit and codeblocks 13.12 with the gcc compiler. here is the one of the three sections of code that uses getline, with some of the extra variables removed. #include <stdio.h> #include <stdlib.h> // For error exit() #include <string.h> char *cmd_buffer

c++ Skip first line of csv file

情到浓时终转凉″ 提交于 2019-12-10 11:48:28
问题 I got my program to read from a .csv file and output the data but I don't want it to output the first line. I've tried to use getline(data, line); and stream.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); . While it does skip the first line, the last two lines print twice and are mixed up. string ID; string sentenceIn; string servedIn; int sentence; int served; string lastName; string firstName; vector<string> idNum; vector<string> sentenceLen; vector<string> servedTime; vector

boost::iostreams, gzip files and tellg

社会主义新天地 提交于 2019-12-10 11:24:35
问题 I have been using boost::iostreams for reading an uncompressed text file. In my application, I need multiple file handles (stored in a map) to efficiently buffer data for different parameters stored in this file. Also, if I read a line and it is for a parameter at a time later than I am currently interested in, I restore the stream position (recovered via tellg()) to where it was before I called getline() so I can still buffer this value at a future time. However, I now wish to read gzip

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

给你一囗甜甜゛ 提交于 2019-12-10 02:26:41
问题 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; } Someone mentioned that this will silently "swallow" errors occurring in std::getline . And of course I agree that's the case. But it occurred to me, what could possibly go wrong here in practice

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

大憨熊 提交于 2019-12-09 13:24:48
问题 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 ss.rdbuf() : hej med dig But why is that? Is that because of ostreams definition of operator<str() gives me different output. In my eyes the output should be the same even if I have used getline. 回答1

Using getline() to read in lines from a text file and push_back into a vector of objects

蹲街弑〆低调 提交于 2019-12-08 11:15:36
问题 I am having issues figuring out how to properly use getline() when it comes to classes and objects. I am needing to read in lines of string type and then add them to the myVec vector using push_back . Here is what I have at the moment: vector<myClass> read_file(string filename) { vector<myClass> myVec; myClass line; ifstream inputFile(filename); if (inputFile.is_open()) { while (inputFile.getline(inputFile, line)) // Issue it here. { myVec.push_back(line); } inputFile.close(); } else throw