-2
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () 
{
string strFile = "";
ifstream myfile ("data.doc");

string strPgm  = "{ \"c\" : { \"p1\": \"v1\", \"p2\":\"v2\" } }";
cout << "\n string from program is : " << strPgm <<"\n"; 
if (myfile.is_open())
{
string line;
while ( getline (myfile,line) )
{
  strFile = strFile+line;
}
myfile.close();
cout <<"\n string from file is  : " << strFile <<"\n";
}

else cout << "Unable to open file"; 
return 0;
}

In my code , backslash is coming in the file output , but it is absent in the output of hard coded variable value in the program.I am not sure , if I am missing something.


The contents of the file data.doc is same as in the variable strPgm, cat data.doc ,

"{ \"c\" : { \"p1\": \"v1\", \"p2\": \"v2\" } }"

But I am getting the output as ,

string from program is :

{ "c" : { "p1": "v1", "p2": "v2" } }

string from file is :

"{ \"c\" : { \"p1\": \"v1\", \"p2\": \"v2\" } }"

Why is this so ? Both are same strings . Because of this, problem is happening while converting strFile in jsonFormat and parsing it.

BusyTraveller
  • 183
  • 3
  • 14

2 Answers2

2

It is the compiler which handles escapes and backslashes in constant string literals. So if you have a string like "Hello\nWorld" in your source code it is the compiler that converts \n to a newline. If you have a file containing backslashes, those are not processed but read as-is.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

You've misunderstood what this line does:

string strPgm  = "{ \"c\" : { \"p1\": \"v1\", \"p2\":\"v2\" } }";

It does not create a string that has backslashes in it. The backslashes are for escaping the double-quotes so that the entire line is considered part of your string to store, instead of stopping at the c.

If you really want to write actual backslashes into a string literal, you'll have to write them like this:

string strPgm  = "{ \\\"c\\\" : { \\\"p1\\\": \\\"v1\\\", ...

i.e. first the backslash, with an escape to make it an actual backslash (lol):

\\

and then the escaped double-quote:

\"

Though it seems really unlikely that you'd want to store escaped JSON.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I know what the strPgm does.But, I didn't understand why the outputs are different when same string is stored in a file.I don't want to write backslashes.In fact , my requirement is that while copying or storing the contents of the file to a variable, and then printing it , it should not contain any backslashes.While I am hard coding, backslash is getting escaped.But while I am reading from the file, the same thing is not happening. – BusyTraveller Jul 11 '16 at 11:46
  • The file already contain the backslash,because the file contains the output of a python program. And I am not able find a way to store the json into a file without backslash. – BusyTraveller Jul 11 '16 at 11:46
  • @Busy: I don't understand your concern. I've explained what your C++ string is. You've said that your file contains something else. Well, then, that's why they're different. Are you _really_ asking how to unescape a string read from a file? Because your question doesn't say that! I've also shown you how to produce a C++ string with literal backslashes included, in case you want that. – Lightness Races in Orbit Jul 11 '16 at 11:47
  • My requirement is that , I want to escape backslash while I am reading from a file.The file contains the same string like that is stored in strPgm. – BusyTraveller Jul 11 '16 at 11:55
  • @BusyTraveller: _"The file contains the same string like that is stored in strPgm"_ No, it doesn't. You told us that, and I've explained why. – Lightness Races in Orbit Jul 11 '16 at 11:56
  • Sorry to correct you , I am pasting the following from the original post , The contents of the file data.doc is same as in the variable strPgm, cat data.doc , "{ \"c\" : { \"p1\": \"v1\", \"p2\": \"v2\" } }" – BusyTraveller Jul 11 '16 at 12:02
  • @BusyTraveller: Sorry to correct you, I am pasting the following from the original post. _«string from program is : `{ "c" : { "p1": "v1", "p2": "v2" } }`»_ How are they the same? I thought you were asking why they are not the same. Saying they are the same then showing that they're not is ... very confusing. At this point I have no idea what your question is. – Lightness Races in Orbit Jul 11 '16 at 12:07