C++/CX Async reading from txt files that are in the .xap/installed folder

Little utility to read txt from a flat file with c++/cx windows 8 and windows phone 8 apps.

inline Platform::String^ LoadTxtFile(Platform::String^ filename)
{
  using namespace Windows::Storage;
  auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation;

  Platform::Array<byte>^ data = nullptr;
  std::wstring dir = folder->Path->ToString()->Data(); 
  auto fullpath = dir.append(L"/").append(filename->Data());

  std::ifstream file(fullpath, std::ios::in | std::ios::ate);
  // if opened read it in
  if(file.is_open())
  {
    int length = (int) file.tellg();
    data = ref new Array<byte>(length);
    file.seekg(0, std::ios::beg);
    file.read(reinterpret_cast<char*>(data->Data), length);
    file.close();
  }

  std::wstring output;
  for(int i=0; i < data->Length; i++)
    output += data[i];
  
  return ref new Platform::String(output.c_str());
}
Advertisements

About kooksta

Professional .NET Developer interested in Games, Automotive, Music.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s