getline

Using get line() with multiple types of end of line characters

点点圈 提交于 2019-12-08 04:09:11
问题 I am using std::getline() in the following manner: std::fstream verify; verify.open(myURI.c_str()); std::string countingLine; if(verify.is_open()){ std::getline(verify, countingLine); std::istringstream iss(countingLine); size_t pos; // Check for the conventional myFile header. pos = iss.str().find("Time,Group,Percent,Sign,Focus"); if(pos == std::string::npos){//its not there headerChk = false; this->setStatusMessage("Invalid header for myFile file"); return 0; } // loop that does more

read strings from input till EOF

别来无恙 提交于 2019-12-08 02:49:48
问题 I have gone through many posts on SO , but still i am not able to resolve the issue . I have to read : text pattern1 pattern2 from standard input , there are many text and patterns . Code : string t,p1,p2; while(getline(cin, t)) { cin>>p1; cin>>p2; cout<<"text is = "<<t<<"\np1 is = "<<p1<<"\np2 is = "<<p2<<endl; } Input file : hammer ham mer gogoa go z gogoa g o Output : text is = hammer p1 is = ham p2 is = mer text is = p1 is = gogoa p2 is = go text is = p1 is = z p2 is = gogoa text is = p1

c getline skip blank line

本秂侑毒 提交于 2019-12-07 20:05:36
问题 while(getline (&line, &line_size, f) != -1){} I'm using this function to read line line. But i want to know when i'm reading a blank line. Can someone help? 回答1: so as H2CO3 already mentioned you can use the line length for this: while (getline (&line, &line_size, f) != -1) { if (strlen(line) == 1) { printf("H2CO3 spotted a blank line\n"); } /* or alternatively */ if ('\n' == line[0]) { printf("Ed Heal also spotted the blank line\n"); } .. } 回答2: You need to define blank line. Also, because

problem using getline with a unicode file

非 Y 不嫁゛ 提交于 2019-12-07 19:31:14
问题 UPDATE: Thank you to @Potatoswatter and @Jonathan Leffler for comments - rather embarrassingly I was caught out by the debugger tool tip not showing the value of a wstring correctly - however it still isn't quite working for me and I have updated the question below: If I have a small multibyte file I want to read into a string I use the following trick - I use getline with a delimeter of '\0' e.g. std::string contents_utf8; std::ifstream inf1("utf8.txt"); getline(inf1, contents_utf8, '\0');

字符串相关函数

痴心易碎 提交于 2019-12-07 11:32:24
strcmp:C++自带函数---字典序 strcpy:交换顺序 getline: 本质上有两种getline函数,一种在头文件 中,是istream类的成员函数。一种在头文件 中,是普通函数。 在 中的getline函数有两种重载形式: istream& getline (char* s, streamsize n ); istream& getline (char* s, streamsize n, char delim ); 作用是从istream中读取至多n个字符保存在s对应的数组中。即使还没读够n个字符,如果遇到换行符'\n'(第一 种形式)或delim(第二种形式),则读取终止,'\n'或delim都不会被保存进s对应的数组中。 在 中的getline函数有四种重载形式: istream& getline (istream& is, string& str, char delim); istream& getline (istream&& is, string& str, char delim); istream& getline (istream& is, string& str); istream& getline (istream&& is, string& str); 用法和上一种类似,不过要读取的istream是作为参数is传进函数的

Why seekg does not work with getline?

拜拜、爱过 提交于 2019-12-07 01:56:55
问题 Seekg does not seem to work, when I reach EOF in myFile. ifstream myFile("/path/file"); for(int i; i < 10; i++){ myFile.seekg(0);//reset position in myFile while(getline(myFile, line)){ doSomething } } So, now I am opening input stream every loop: for(int i; i < 10; i++){ ifstream myFile("/path/file");//reset position in myFile while(getline(myFile, line)){ doSomething } } But I would rather seek to position 0. How can I achieve that? 回答1: Make sure you clear the error flags before the call

C++ getline multiple variable types using comma as delimiter

試著忘記壹切 提交于 2019-12-06 13:47:07
问题 I'm trying to do a home work assignment which requires data fro a txt file to be read in to variables. The file has this on each line "surname, initials, number, number". I have got the get line working partially using the following code. ifstream inputFile("Students.txt"); string line; string Surname; string Initial; int number1, number2; while (getline(inputFile, line)) { stringstream linestream(line); getline(linestream, Surname, ','); getline(linestream, Initial, ','); getline(linestream,

Unexpected behaviour of getline() with ifstream

我的梦境 提交于 2019-12-06 13:01:50
To simplify, I'm trying to read the content of a CSV-file using the ifstream class and its getline() member function. Here is this CSV-file: 1,2,3 4,5,6 And the code: #include <iostream> #include <typeinfo> #include <fstream> using namespace std; int main() { char csvLoc[] = "/the_CSV_file_localization/"; ifstream csvFile; csvFile.open(csvLoc, ifstream::in); char pStock[5]; //we use a 5-char array just to get rid of unexpected //size problems, even though each number is of size 1 int i =1; //this will be helpful for the diagnostic while(csvFile.eof() == 0) { csvFile.getline(pStock,5,','); cout

Read table of numbers into arrays when number of rows and columns determined at runtime

血红的双手。 提交于 2019-12-06 12:20:29
问题 I would like to ask you about data input. I have a text file in the following format: 7 2 X Y 1 0 2 0.048922 3 0.0978829 4 0.146908 5 0.196019 6 0.245239 7 0.294584 The first line contains the number of rows and columns to be read in. The second line are headers. From the third line onwards it's only data. I would like to read my data into a 2D array (mat[][]) and the headers into an array of strings (title[]), which could be easily referenced later. I came this far writing the script. It can

boost::iostreams, gzip files and tellg

Deadly 提交于 2019-12-06 10:43:25
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 compressed files, but otherwise perform identical operations as before. I have run across the following