stringc++11winapicouttdm-gcc

C++ cout or wcout a string contain "\n" isn't split the lines


C++11 and compiler is TDM-GCC.

I made an ini file and read string from it(UCS-2 LE BOM), by WinAPI, GetPrivateProfileStringW.

[String]
Example = Today\nYesterday\nApple

My function library will return a variable, std::wstring. And it looks fine. Then use wcout, just like:

#include <iostream>
#include <string>

using namespace std;
wstring readString = GetPrivateProfileStringW();    // Hide the details.
wcout << readString << endl;

And what do I get on my screen:

Today\nYesterday\nApple

What I want:

Today
Yesterday
Apple

I'm not sure why the "\n" is not work on this situation.

To avoid it, I can create multiple INI key and cout them using "endl".

But in this project, how many lines around here should be dynamic.

How do I cout the single string variable from Windows API on screen, become multiple lines?


Solution

  • The sequence \n is only recognized by the compiler in source code as part of a literal string or character. It's nothing that is built into the library or any standard functions.

    If you want the sequence to represent a newline inside input you have read from an external source, you have to handle it yourself.