read-only error with string

Maybe someone can help me with this. I reduced the code a bit:

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
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.)
Last edited on
workQ is not read only, however workQ.c_str() returns a const C-string. Why are you trying to convert the std::string to a C-string?
Actually my original code is like this:
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
29
30
31
void replaceWith(const char * 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';

Thanks for your help thus far.
You're doing the same thing. You're passing a const C-string, then trying to change it (line 5). That's a no-no.

Change replaceWith as follows:
1
2
3
4
5
6
void replaceWith (string & que)  // pass string by reference
{  for (std::size_t i = 0; i < que.size(); ++i)
    {  if (que[i] == '1') 
          que[i] = 'b';
    }
}


Then at line 26:
 
  replaceWith (workQ);

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.
You could also replace the loop in your replace function with a ranged based loop or a call to replace_if().

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
29
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>

void replaceWith(std::string& que)
{
    // Ranged based loop.
    for(auto& itr : que)
        if(itr == '1')
            itr = 'b';

    // call replace_if() from  <algorithm> 
    std::replace_if(que.begin(), que.end(), [](char i){return i == '1';}, 'b');
}

int main(){
    //The main string to work with
    std::string workQ = "1010100";



    replaceWith(workQ);

    std::cout << workQ << std::endl;

return 0;
}


Topic archived. No new replies allowed.