How to read a file on Hololens with C++ - c++

I am trying to read a file in a test/debug UWP application that is being deployed to Hololens. I can put the file on the device with the device portal, but am unable to find the correct path to open the file.
I am using the MSFT BasicXrApp_uwp example as a basis, and have included FileUtility which has a FindFileInAppFolder function. This is consistently failing to find the file, with the error:
"The file should be embeded in app folder in debug build.",
after letting me know the app folder is:
C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\364f83f4-6e13-42e4-8253-71dd3040951cVS.Debug_ARM.mikeh\
The part 364f83f4-6e13-42e4-8253-71dd3040951cVS is recognisable in the device portal as the User Folders/LocalAppData folder, but the Debug_ARM.mikeh part is not visible on the portal.
I am using C++ and trying to do the file reading in a static, non uwp library if possible (pointing that out so I don't get suggestions to use UWP async stuff, if that is possible).
So, how do I embed my file in the app folder, or how do I place the file so I can read it?

This is because the folder path which FindFileInAppFolder method returns is the InstalledLocation of the current package, but what you checked in the device portal is LocalFolder/LocalCacheFolder, for more information about what different between them please see: File access permissions.
how do I embed my file in the app folder, or how do I place the file so I can read it?
You can place your file in the LocalState folder by Device Portal and get this folder path through ApplicationData.LocalFolder Property, the path should be like: C:\Data\Users\DefaultAccount\AppData\Local\Packages\364f83f4-6e13-42e4-8253-71dd3040951c\LocalState. For how to access the files via C++ you can use for example File access sample

I'm using an answer here as there's more room than a comment.
I found a few extra things useful. I added in the cppWinRT nuget package to my application.
I did need to use the "async stuff", for example:
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage;
StorageFolder storageFolder= KnownFolders::GetFolderForUserAsync(nullptr, KnownFolderId::PicturesLibrary).get();
This let me find a file I'd uploaded ot the Pictures Library. But I couldn't open it after passing the path to my existing library:
const auto sampleFile = storageFolder.GetFileAsync(fileName).get();
std::wstring path = sampleFile.Path();
MyLibraryCall(to_string(path));
MyLibraryCall would try and open an ifstream, and even using std::ifstream::in would fail.
So I copied the file to the temp directory, where I could open it and process it.
This is pretty hacky but it did what I needed, which is let me load an .obj file that was rejected by the 3D parts viewer.
The loop over all filenames is because storageFolder.GetFileAsync(fileName).get() throws an exception if it fails, which for me I could not catch properly.
StorageFolder tempFolder = Windows::Storage::ApplicationData::Current().TemporaryFolder();
std::wstring path;
auto files = tempFolder.GetFilesAsync().get();
for (auto file : files)
{
if (file.Name() == fileName) {
path = file.Path();
break;
}
}
if (!path.size()) {
// hasn't been copied into temp
StorageFile movedFile = sampleFile.CopyAsync(tempFolder).get();
path = movedFile.Path();
}
MyLibraryCall(to_string(path));
Anyway- not the greatest but that will hopefully help someone else looking for a quick and dirty way to process a file on a hololens/UWP app.

Related

How do you write a file in a specific location on iPhone?

I have been trying to create and write a file using an iPhone C++ compiler, but the file always ends up at a location called /tmp, which I do not have access to. Is there any way to use the iPhone to create and write a file to a specific path location?
I have tried to use the following code, but it does not create the file at that location:
#include <iostream>
#include <fstream>
int main(){
std::ofstream File("/private/var/mobile/Containers/Data/Application/8EDE34D7-84CE-4147-8126-8D1AF04717A1/Documents/my_folder/my_file.txt");
File << "Hello!";
File.close();
return 0;
}
Your code ignores state of your File object. It just blindly attempts to write into it so no one knows what went wrong when opening it. There are lot of things that can go wrong but you should add code to check its state on any platform.
Two most common things that can go wrong:
Is /private/var/mobile/Containers/Data/Application/8EDE34D7-84CE-4147-8126-8D1AF04717A1 certainly home directory of your app? You can not access files of another app, if you are trying to do that. Normally you should request your app home directory from operating system, with CFCopyHomeDirectoryURL or perhaps getenv works too:
std::string home = getenv("HOME");
If it is directory of your app then you still can not create files in directory my_folder if that directory inside Documents directory of your app does not exist. For that you need to use NSFileManager or perhaps mkdir from sys/stat.h works too.

Opening files on Android using Cocos2d-x api

I am having difficulty opening a file with cocos2d-x FileUtils in an android project. I am using Cocos2d-x v2.2.3 and the create_project.py to set up my project structure. There is a folder called Resources that contains some HelloWorld png's. I would like to add other files to this folder and then perform read operations with standard file stream operations. However, opening the file fails when running on the Android emulator. I have tried using CCFileUtils::sharedFileUtils()->getWritablePath() and then appending the name of the file. What is the correct way to use Cocos2d-x to open files on android?
The folder Resources contains files for which the app has only a read access.
To get the correct path to these files use:
string yourFilename = "hello.txt"; // only (relative path+) filename here, no absolute path!
string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(yourFilename.c_str());
Open the file using fullPath!
The routine searches in a number of paths which can be extended.
See description of CCFileUtils::fullPathForFilename()
To define the search paths and the search order write something like:
vector<string> searchPaths;
searchPaths.push_back("1st_path");
searchPaths.push_back("2nd_path");
searchPaths.push_back("3rd_path");
CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(searchPaths);
For Android apps the Resource folder is already part of the search paths, so no need to add it here.
To see the different paths try:
CCLOG ("full path: %s", fullPath.c_str());
CCLOG ("writable path: %s", CCFileUtils::sharedFileUtils()->getWritablePath().c_str());

Open file coming from an invoke request on Blackberry 10 Cascades

I am writing a Blackberry 10 Cascades app. I am trying to open a file which is an email attachment.
int in_fd = open(m_uri.replace("file://","").toUtf8().constData(), O_RDONLY);
(There's a reason I am using the open() call)
But the file cannot be opened. I understand that this is to do with sandboxing and app permissions, and that the Invocation framework can make a copy of the file in the target app's directory. What I can't figure out is how to get the path to that file so that I can open it.
I need to know:
How to make sure the Invocation Framework copies the file to my apps sandbox on invocation.
How to access the resulting copied file.
In C++
Can you print an example of what is contained inside m_uri when your code runs?
If the path you are feeding open() does not have a leading /, then it will look from the app's home directory which is not what you want.

VS2010 doesnot pick up file from resources folder

I am required to parse a text file in my VS project in mfc in c++. The text file is supposed to be a part of the entire exe product. For that purpose, I placed the text file in my resources folder and set the path in my code as:
char fileName[] = "../myFile.txt";
The problem I'm facing is that VS doesn't find this file in its Resources folder. I added the file in the project file, but that just gave me a corrupt file error. However, the file access works if I provide the absolute path to the file in my code i.e. "C/abc/myFile.txt"
I need the code running on all machines, hence need some method to get VS to read this file using a relative path. Can anybody please provide some assistance? I am a newbie and have tried all that's in my knowledge.
Actually, if it's a resource file it should be copied over to the bin folder, which means your fileName should just be:
char fileName[] = "myFile.txt";
if that doesn't work then, you might need to change the properties of your myFile.txt to ensure it does get copied over with the build process.
Here you can find an answer for your question: http://www.cplusplus.com/forum/general/54255/

Xcode loading program resources from my Home folder rather than Resources - C++

This is my first time using Xcode and it is appalling to me how completely non intuitive this IDE is. I heard it was better in the past and I really hope it was.
My problem is that the resources my program loads, a data file and an .ini file, it automatically searches for these file in my Home folder. I want for it to search for these files in the Resource folder of the .app. I am using C++ and all of the examples I have found are for Objective-C.
Any idea on how to fix this?
You are probably assuming that, when your app launches, the current working directory of the process is your app's bundle. It isn't. (Nothing to do with Xcode particularly -- that's just how OS X works.)
Typically you would use NSBundle (Objective-C) or CFBundle (C)
to find resources in your app bundle. Since you're using C++, let's use the C API.
To find the URL to a file "myFile.ini" in the Resources directory in your app bundle:
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef url = CFBundleCopyResourceURL(mainBundle, CFSTR("myFile"), CFSTR("ini"), NULL);
UInt8 filePath[PATH_MAX];
if (CFURLGetFileSystemRepresentation(url, true, filePath, sizeof(filePath)))
{
// use your API of choice to open and read the file at filePath
}
Or, to just change the CWD to your app bundle:
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef url = CFBundleCopyBundleURL(mainBundle);
UInt8 bundlePath[PATH_MAX];
if (CFURLGetFileSystemRepresentation(url, true, bundlePath, sizeof(bundlePath)))
{
if (chdir((const char*)bundlePath) == 0)
{
// now the CWD is your app bundle, and you can use relative path names to access files inside it
}
}