-2

I have this code which runs fine

#include <iostream>
#include <set>
#include <sstream>

int main()
{   
    std::set<std::string> a;
    a.insert("foo");
    a.insert("bar");
    a.insert("zoo");
    a.insert("should");
    a.insert("work");
    std::stringstream b;
    std::set<std::string>::iterator it;
    for (it = a.begin(); it != a.end(); it++)
    {
        b << " " << *it <<"," <<"\n";

    }

    std::string aaa = b.str();
    std::cout <<aaa;
}

Output in command prompt:

bar, //new line after ","
foo, //new line after ","
should,
work,
zoo,

If I try to write the same string aaa in file I am expecting the same output to get print in the file i.e. every string after "," in new line, rather I am getting output in my file as follows (In single line with \n):

" bar,\n foo,\n should,\n work,\n zoo,\n"

Can anyone help me with this?

More Information on writing the string in file:

Here's how I am writing into file:

boost::property_tree::ptree pt1;
pt1.put( "Output", aaa );
boost::property_tree::write_json( "result.json", pt1 );

This will write JSON file, output of the above code in (Windows - NotePad/NotePad++) is as below:

{
    "Output": " bar,\n foo,\n should,\n work,\n zoo,\n"
}
Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
silent_programmer
  • 728
  • 2
  • 7
  • 18

2 Answers2

3

You are not writing a normal file! You are using a JSON library to write a file for you. And as it happens, in JSON strings, the end-of-line character is escaped just like in C source files, that is as "\n".

So, summing up, that is the expected behavior. If you want to get normal end-of-line characters, write a normal file, with fopen() and friends.

rodrigo
  • 79,651
  • 7
  • 121
  • 162
  • I am working on getting JSON as ouput, its easy to use boost library rather than creating a file. Is there any way normal EOL characters can be used with this Boost JSON library? – silent_programmer Feb 06 '17 at 20:04
  • @silent_programmer: Those requirements don't make sense, The LF character is not valid inside a JSON string, it must be encoded as `\n` (or `\u000a`). If you want to see your text divided in proper lines you could use a JSON viewer. I don't know of any but sure there are a few out there... A normal text editor will not show you that. – rodrigo Feb 06 '17 at 20:08
  • @silent_programmer: If the JSON theme is not a hard requirement, take a look at [this question](http://stackoverflow.com/questions/5257049/what-is-the-most-elegant-way-to-write-a-text-binary-file-with-c). – rodrigo Feb 06 '17 at 20:11
  • Thanks for the information but JSON is the requirement. – silent_programmer Feb 06 '17 at 20:24
1

This is expected behaviour.

You are passing the string (which contains newlines) to a JSON library for encoding into JSON. That encoding step includes converting newlines to the substring "\n", because that's how we represent newlines inside strings in JSON.

Read more about JSON on the json.org website.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989