istringstream

how to read and parse text files with istringstream?

♀尐吖头ヾ 提交于 2021-01-29 08:05:00
问题 I am trying to read a .txt file line by line: vector<vector<int>> iVecGrid; vector<int> iVecRow; ifstream text; text.open("grid.txt", ios::in); if (!text) { cout << "Error!\n"; return EXIT_FAILURE; } istringstream isLine; string sLine; while (getline(text, sLine)) { isLine.str(sLine); int iNumber; while (isLine >> iNumber) { cout << iNumber << " "; iVecRow.push_back(iNumber); } cout << endl; iVecGrid.push_back(iVecRow); iVecRow.clear(); } When I put isLine inside the while (getline(text,

Using istringstream in C++

余生颓废 提交于 2020-03-22 09:16:10
问题 I have some code that utilizes fork, execlp, and wait to make two processes. The objective is to be able to repeatedly print a prompt and have the user enter a command with up to 4 arguments/options to the command. int main() { string command, argument; istringstream iss(argument); do { //prompt user for command to run cout << "Enter command: "; cin >> command >> argument; int pid, rs, status; //fork will make 2 processes pid = fork(); if(pid == -1) { perror("fork"); exit(EXIT_FAILURE); } if

non-copying istringstream

丶灬走出姿态 提交于 2020-01-23 04:46:11
问题 So istringstream copies the contents of a string when initialised, e.g string moo("one two three four"); istringstream iss(moo.c_str()); I was wondering if there's a way to make std::istringstream use the given c_str as its buffer without copying things. This way, it won't have to copy large bits of memory before passing the std::istringstream& to functions that take istream& as an argument. What I've been trying to do is convert some functions which only take std::ifstream& arguments (they

The role of std::ws (whitespace) when reading data

你。 提交于 2020-01-01 14:23:22
问题 Data saved in my file is (white spaces added at both beginning and end on purpose for this test): 1 2 3 Loading the data using the code below with or without "std::ws" does not cause any difference. So I am confused by the role of "std::ws" as I have seen code using it. Can someone explain a little bit? Thanks! void main () { ifstream inf; inf.open ("test.txt"); double x=0, y=0, z=0; string line; getline(inf, line); istringstream iss(line); //Using "std::ws" here does NOT cause any difference

C++ compile error: has initializer but incomplete type

孤者浪人 提交于 2019-12-20 15:36:17
问题 I am coding in Eclipse and have something like the following: #include <ftream> #include <iostream> void read_file(){ char buffer[1025]; std::istringstream iss(buffer); } However, when I try to build, I get the following error: variable 'std::istringstream iss' has initializer but incomplete type Any quick thoughts? I have googled around and it seems like most people with this problem simply did not include the right header files which I believe I am doing correctly. 回答1: You need this

Internal buffer used by standard input stream (pubsetbuf)

…衆ロ難τιáo~ 提交于 2019-12-20 06:38:34
问题 I'm trying to set the internal buffer of an input stream, but my implementation in C++17 doesn't implement pubsetbuf() for istringstream. I've tried some other techniques, but they are slow or copy the original buffer. I'm looking for a fast method that doesn't do any copying. It's closely related to this question about an output stream: Setting the internal buffer used by a standard stream (pubsetbuf) I've followed it closely, but the buffer for the input stream remains uninitialised/empty.

Passing temporary istringstream object to istream_iterator<string>

白昼怎懂夜的黑 提交于 2019-12-20 02:26:13
问题 I have a question about the following code that tokenizes a string (separates the tokens by space). #include <iostream> #include <iterator> #include <sstream> #include <string> #include <vector> using namespace std; int main() { string s="And I feel fine ..."; istringstream ss(s); vector<string> tokens{istream_iterator<string>(ss),{}}; for(auto& elem: tokens) cout << elem << endl; } This works perfectly fine. On the other hand, if I try to pass a temporary istringstream object to istream

How to extract mixed format using istringstream

谁说我不能喝 提交于 2019-12-19 19:46:41
问题 Why does my program not output: 10 1.546 ,Apple 1 instead of 10 1 <empty space> here's my program: #include <iostream> #include <string> #include <sstream> using namespace std; int main () { string str = "10,1.546,Apple 1"; istringstream stream (str); int a; double b; string c, dummy; stream >> a >> dummy >> b >> dummy >> c; cout << a << endl; cout << b << endl; cout << c << endl; return 0; } Basically I am trying to parse the comma-separated strings, any smoother way to do this would help me

Storing text file into a class

寵の児 提交于 2019-12-13 20:17:33
问题 I am currently trying to read in characters into a class that looks like this struct data { string segment; string name; double length; double radius; double wall_thickness; double young_modulus; double compliance; }; I also have a vector that contains the following elements: 2A Aorta_Ascendens 2 1.47 .164 4 53.4 2B Aorta_Ascendens 2 1.44 .161 4 51.0 3A I want to read in the text file into each part, and currently this is my algorithm to continuously read through the text file and add each

How do I check that stream extraction has consumed all input?

久未见 提交于 2019-12-13 13:06:49
问题 In the following function, I try to see if a string s is convertible to type T by seeing if I can read a type T , and if the input is completely consumed afterwards. I want template <class T> bool can_be_converted_to(const std::string& s, T& t) { std::istringstream i(s); i>>std::boolalpha; i>>t; if (i and i.eof()) return true; else return false; } However, can_be_converted_to<bool>("true") evaluates to false, because i.eof() is false at the end of the function. This is correct, even though