This sample demonstrates the writing of text files using nn::nlib::FileOutputStream
. It reads a file written via a stream and displays the content on the console.
Writing text through a nn::nlib::TextWriter
object allows the text to be converted from UTF-16 to UTF-8 before being written. The BOM is not written. In addition, newline characters are integrated into CRLF.
To directly write binary data, direct use is made of a stream object for writing.
The process is the same when streams other than file streams are used for reading; the only difference is the stream being used.
#include <string>
const wchar_t text[] =
L"Running in the circle is the green Yamanote line\n"
L"円く走るのが緑の山手線\n";
NLIB_PATHMAPPER_FORSAMPLE
bool SampleMain(int, char**) {
InitPathMapperForSample();
char filename[1024];
g_PathMapper.ResolvePath(NULL, filename, "nlibpath:///readwrite/writefile.txt");
{
FileOutputStream stream;
if (0 != stream.Init()) return false;
if (0 != stream.Open(filename)) {
return false;
}
{
TextWriter writer;
writer.Init(&stream);
if (!writer.Write(text)) return false;
}
if (!stream.Close()) return false;
}
std::string str;
FileInputStream stream;
if (0 != stream.Init()) return false;
if (0 != stream.Open(filename)) {
return false;
}
int c;
while ((c = stream.Read()) > 0) {
str += static_cast<char>(c);
}
if (stream.GetErrorValue() != 0) {
return false;
}
return true;
}
NLIB_MAINFUNC