locked
Getting a legal directory I can save data to (legacy code)

    Question

  • Hi there,

    I'm bringing a bunch of portable/legacy code over that uses C style File IO (fopen, fwrite etc). I realize we should be moving to asynchronous File IO, but it's just not practical for this.

    How can I get a legal directory that I can append to an "fopen" path where I can save user data?

    I'll admit, I don't fully understand all the ^ stuff, so a code example would be appreciated.

    Thanks!

    Sunday, September 30, 2012 4:39 PM

Answers

  • The following will give you a path to AppData that you can use with _wfopen_s:

    static wchar_t wzAppDataDirectory[MAX_PATH];
    
    //-------------------------
    // Call at app startup
    
    void InitializeDirs()
     {
      wcscpy_s( wzAppDataDirectory, MAX_PATH, ApplicationData::Current->LocalFolder->Path->Data() );
      wcscat_s( wzAppDataDirectory, MAX_PATH, L"\\" );
     }
    
    
    Append a filename to wzAppDataDirectory to create a full path and call _wfopen_s() to get a FILE *. I hide all that behind a function, FILE *OpenAppDataFile( char *szFilePart ), to use in my legacy code.

    • Edited by henador Sunday, September 30, 2012 9:44 PM
    • Marked as answer by Mike Kasprzak Monday, October 01, 2012 10:03 PM
    Sunday, September 30, 2012 9:42 PM

All replies

  • fopen is not recommended for Windows Store apps. You can use Windows::Storage::FileIO class instead.

    In Windows Store apps, you cannot access any file system rather than can access certain file system locations, like the app install directory, app data locations, and the Downloads folder. Following links may help to you.

    File access and permissions in Windows Store apps

    File access sample


    • Proposed as answer by Jesse Jiang Monday, October 01, 2012 6:46 AM
    • Unproposed as answer by Mike Kasprzak Monday, October 01, 2012 9:51 PM
    Sunday, September 30, 2012 5:43 PM
  • The following will give you a path to AppData that you can use with _wfopen_s:

    static wchar_t wzAppDataDirectory[MAX_PATH];
    
    //-------------------------
    // Call at app startup
    
    void InitializeDirs()
     {
      wcscpy_s( wzAppDataDirectory, MAX_PATH, ApplicationData::Current->LocalFolder->Path->Data() );
      wcscat_s( wzAppDataDirectory, MAX_PATH, L"\\" );
     }
    
    
    Append a filename to wzAppDataDirectory to create a full path and call _wfopen_s() to get a FILE *. I hide all that behind a function, FILE *OpenAppDataFile( char *szFilePart ), to use in my legacy code.

    • Edited by henador Sunday, September 30, 2012 9:44 PM
    • Marked as answer by Mike Kasprzak Monday, October 01, 2012 10:03 PM
    Sunday, September 30, 2012 9:42 PM
  • Great, thank you. It was the ->Data() part I was missing in my own tests.
    Monday, October 01, 2012 10:04 PM