-3

what I am doing is writing a .js file with c++ and it will be used in a .html file to draw an organizational chart. I am actually making an abstract syntax tree. for this, I write the tree nodes in a js file like the following

var nodes = [
    "int main(){", 
    "String str = "hello 'Jane'";", 
    "}"
]

there is a problem with the quotes. How do I get the following output with cpp.

var nodes = [
    "int main(){", 
    "String str = \"hello \'Jane\'\";", 
    "}"
]
  • do not get your question – Tarek Salah uddin Mahmud Dec 25 '16 at 06:59
  • In C++ you escape special characters in strings just as you do in JavaScript, with the backslash. If you need more backslashes then you need to escape that too by adding more backslashes. – Some programmer dude Dec 25 '16 at 07:02
  • 1
    http://en.cppreference.com/w/cpp/language/escape as well as numerous existing stackoverflow questions have the answers you need. – TheUndeadFish Dec 25 '16 at 07:51
  • You just have to go with nested backslashes. So, add one more backslash where your target char is already inside another backslash-bound range. "My string has \"some \\\"backslashes\\\"\". You need to backslash the backslash, too. – Mostafa Talebi Dec 25 '16 at 08:10

2 Answers2

-1

for read and write json file you can use boost or QT, I have usually used QT. for example:

QJson json;
//write jeson
json["test1"] = "hello";
json["test2"] = "jan";
//read from json file
cout<<json["test1"];
cout<<json["test2"];
Hamidreza
  • 81
  • 1
  • 6
-1

Write a function called "escape".

  int escape(char *out, size_t sz, const char *in)
  {
     int i = 0;
     int j = 0;

     for(i=0;in[i];i++)
     {
        if(j > sz - 2)
          /* output buffer too small */
        switch(in[i])
        {
           case '\n'; out[j++] = '\\'; out[j++] = 'n'; break;
           case '\\'; out[j++] = '\\'; out[j++] = '\\'; break;

           ...

            default: out[j++] = in[i]; break; 
        }
     }
     out[j++] =0;
     return j;
  }
Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18