how to reuse istringstream in cplusplus [duplicate] - c++

This question already has answers here:
How to initialize a std::stringstream?
(2 answers)
Closed 6 years ago.
I want to reuse istringstream variable. It is easy to initial istringstream variable using construction function. But when I try to re-assign it to a new value using = or <<, I got error. I am using c++11. It seems no compile error in vs 2015 using =. But gcc 4.8.2 (devtools-2 on Centos 6.4 x86_64) get error.
Codes like below:
std::string line;
int simmulationtimes; double InitialIDensity;
// deploy configuration file
std::ifstream config(configfile);
if (!config.is_open())
{
return false;
}
std::getline(config, line);
std::istringstream sline(line);
std::string sInitialIDensity;
while(std::getline(sline, sInitialIDensity, '='));
InitialIDensity = std::stod(sInitialIDensity);
std::getline(config, line);
std::string ssimmulationtimes;
sline.str(""); sline.clear();
sline = std::istringstream(line);
while (std::getline(sline, ssimmulationtimes, '='));
simmulationtimes = std::stoi(ssimmulationtimes);
configuration file should be:
IDensity=0.5
times=5
Error:
error: no match for ‘operator=’ (operand types are ‘std::istringstream {aka std::basic_istringstream<char>}’ and ‘std::string {aka std::basic_string<char>}’)
sline = std::istringstream(line);
Solutions (like how-to-initialize-a-stdstringstream) on stackoverflow about reusing stringstream not work for me. Any ideas about reusing istringstream? Thanks for your consideration.

Use the str() method to set a new std::string into an existing std::istringstream.

Related

std::stringstream reasignment works on Visual Studio 2013 but not under Linux [duplicate]

This question already has answers here:
Move or swap a stringstream
(2 answers)
Closed 7 years ago.
I was just trying to compile the following code on Linux after having perfect success on Windows:
std::string str = stream.str();
auto decrement = [](char c) { return c - 100; };
std::transform(str.begin(), str.end(), str.begin(), decrement);
stream = std::stringstream(str); // LINE ACCUSING ERROR
The error I receive for trying to reasign the std::stringstream is:
158: error: use of deleted function 'std::basic_stringstream&
std::basic_stringstream::operator=(const
std::basic_stringstream&)' stream = std::stringstream(str);
^
std::stringstream is not copyable, but can be only moved (since C++11). My guess is that you use g++4.9 or earlier, which even if it supports C++11, it doesn't fully support the move semantics for streams. g++5 and later compiles your code.
Reported bug dates back to 4.7, fixed in 5.x https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316

C++ No viable conversion from string to const char * [duplicate]

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 7 years ago.
I'm using C++ (using CERN's ROOT framework) and I'm having a little problem with strings. I'm trying to label a histogram axis using a string defined by the user earlier in the code. Here are the relevant parts of the code:
string xlabel;
...
cout << "Enter x-axis label:" << endl;
getline(cin >> ws, xlabel);
...
hist->GetXaxis()->SetTitle(xlabel);
Where the last line is just syntax that ROOT uses (usually xlabel here would be in quotation marks and you can type in what you want the label to be, but I am trying to input the string defined earlier in the code.)
Anyway, when I compile this, I get the following error:
error: no viable conversion from 'string'
(aka 'basic_string<char>') to 'const char *'
hist->GetXaxis()->SetTitle(xlabel);
^~~~~~
I have tried re-defining xlabel as a const char * but it didn't like that either. Does anyone have any suggestions on how I could define this string?
Thanks in advance!
Do this:
hist->GetXaxis()->SetTitle(xlabel.c_str());
// ^^^^^^^^

MoveFileA() doesn't like my arguments [duplicate]

This question already has answers here:
How to convert std::string to LPCSTR?
(9 answers)
Closed 8 years ago.
I have a list of file names in a .txt document, and I would like to move each of these files from one folder to another.
Using MoveFileA() I am getting the error, "no suitable conversion between std::string and LCPSTR".
Here is my code, after opening up my .txt file:
while (std::getline(myfile, line))
{
std::string oldLocation = "C:\\Users\\name\\Desktop\\docs\\folder1\\" + line;
std::string newLocation = "C:\\Users\\name\\Desktop\\docs\\folder2\\" + line;
MoveFileA(oldLocation, newLocation);
}
If I type in the full path as arguments for MoveFileA, instead of sending it a variable, it works but I am unable to iterate over .txt file this way.
Any suggestions on how I might fix this?
LCPSTR means long constant pointer to a string, which means it's a null terminated c string.
std::string is an object. It is something different. But it luckily provides a convenience method c_str the provides a pointer to a constant c style string. So as the comment says you should go by:
MoveFileA(oldLocation.c_str(), newLocation.c_str());
It is worth of explicitly noting, that you can't drop it in every place instead of char*, but only when the string won't be modified. It returns const char*. This is where the C in LCPSTR gets important.

ifstream::open() function using a string as the parameter [duplicate]

This question already has an answer here:
No matching function - ifstream open()
(1 answer)
Closed 7 years ago.
I'm trying to make a program that asks for the file that they user would like to read from, and when I try to myfile.open(fileName) I get the error: "no matching function for call to std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'" at that line.
string filename;
cout<<"Enter name of file: ";
cin>>filename;
ifstream myFile;
myFile.open(filename); //where the error occurs.
myFile.close();
In the previous version of C++ (C++03), open() takes only a const char * for the first parameter, instead of std::string. The correct way of calling it would then be:
myFile.open(filename.c_str());
In current C++ (C++11) that code is fine, though, so see if you can tell your compiler to enable support for it.

How to convert Integer to string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
How to convert a number to string and vice versa in C++
Append an int to a std::string
I want to convert integer to string, any one help me for these conversion?
itoa(*data->userid,buff1,10);
itoa(*data->userphone,buff2,10);
For C++, use std::stringstream instead.
#include <sstream>
//...
std::stringstream ss;
ss << *data->userid;
std::string userId = ss.str();
or std::to_string if you have access to a C++11 compiler.
If you have a C++11 compiler with the new std::to_string function you can use that. Otherwise use the std::stringstream solution by Luchian.