nlib
misc/readfile/readfile.cpp

nn::nlib::FileInputStreamを利用してテキストファイルを読むサンプルです。 ファイルをオープンしてストリームから読み込みコンソールに表示します。

テキストはnn::nlib::TextReaderオブジェクトを通して読むことでUTF-8からUTF-32に変換されたものが利用可能です。 バイナリデータを直接読み込む場合はストリームオブジェクトを直接利用して読み込みます。

ファイル以外のストリームを利用して読み込む場合もそれぞれのストリームを利用すること以外は同様です。

/*--------------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)Nintendo All rights reserved.
These coded instructions, statements, and computer programs contain proprietary
information of Nintendo and/or its licensed developers and are protected by
national and international copyright laws. They may not be disclosed to third
parties or copied or duplicated in any form, in whole or in part, without the
prior written consent of Nintendo.
The content herein is highly confidential and should be handled accordingly.
*--------------------------------------------------------------------------------*/
#include <string>
NLIB_PATHMAPPER_FORSAMPLE
bool SampleMain(int, char**) {
// Initializes g_pathmapper which NLIB_PATHMAPPER_FORSAMPLE defines
InitPathMapperForSample();
// converts an URI to the corresponding native path
char filename[1024];
g_pathmapper.ResolvePath(NULL, filename, "nlibpath:///readonly/readfile.txt");
std::string str;
FileInputStream stream;
nlib_printf("Reading: \'%s\'\n", filename);
if (nlib_is_error(stream.Init())) return false;
if (nlib_is_error(stream.Open(filename))) return false;
TextReader reader;
reader.Init();
reader.Open(&stream);
int c;
while ((c = reader.Read()) > 0) {
// The return value of TextReader::Read() is in UTF-32(UCS-4),
// but readfile.txt is in ASCII.
str += static_cast<char>(c);
}
if (nlib_is_error(reader)) {
return false;
}
nlib_printf("%s", str.c_str());
return true;
}
NLIB_MAINFUNC