int main(){
//The main string to work with
std::string workQ;
//load the text file and put it into a single string:
std::ifstream inputFile("a.txt");
std::stringstream buffer;
buffer << inputFile.rdbuf();
std::string bufString = buffer.str();
std::stringstream strstr;
for (std::size_t i = 0; i < bufString.size(); ++i)
{
strstr << std::bitset<8>(bufString.c_str()[i]);
}
workQ = strstr.str();
for(std::size_t i = 0; i < workQ.size(); ++i)
{
// Error below...
if(workQ[i] == '1') workQ.c_str()[i] = 'b';
}
std::cout << " DONE..." << std::endl;
return 0;
}
When trying to compile i get this:
error assignment of read-only location ‘*(que + ...
if(que[i] == '1') que[i] = 'b';
Why is that? (I really don't know why workQ is read-only.)
void replaceWith(constchar * que, std::size_t qsize)
{
for(std::size_t i = 0; i < qsize; ++i)
{
if(que[i] == '1') que[i] = 'b';
}
}
int main(){
//The main string to work with
std::string workQ;
//load the text file and put it into a single string:
std::ifstream inputFile("a.txt");
std::stringstream buffer;
buffer << inputFile.rdbuf();
std::string bufString = buffer.str();
std::stringstream strstr;
for (std::size_t i = 0; i < bufString.size(); ++i)
{
strstr << std::bitset<8>(bufString.c_str()[i]);
}
workQ = strstr.str();
replaceWith(workQ.c_str(), workQ.size());
std::cout << " DONE..." << std::endl;
return 0;
}
I'm calling the function replaceWith() with the string workQ as argument. As i was told to pass this one should use converting with c_str()...
I get the same error:
error: assignment of read-only location ‘*(que + ((sizetype)i))’
if(que[i] == '1') que[i] = 'b';
Okay, i completely understand and i will do it the way you suggested. Strange that c_str() returns constant - i didn't know and i didn't read about it. Anyway, thanks for your help.