c++cfileuwpc++-cx

UWP - Write file using C codes


I have a C++ UWP app that calls a C function which writes to a file .e.g.

FILE * fp = fopen ("file.txt", "w+");
fprintf(fp, "Hello");
fclose(fp);

fopen() failes with error 13 which means that there are insufficient permission on the directory. How can I ensure that the C function can write to a file?


Solution

  • Since UWP runs in a sandbox, I solved it by getting a writable file path in C++:

    StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;
    String^ path = localFolder->Path;
    

    Then I passed the path to the C program.