#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.