简体   繁体   中英

Windows Store C++ apps cannot create files using fopen() under Windows 8.1

We have several Windows 8 Store C++ apps that need to maintain configuration and data files.

Files are written in subfolders of Windows::Storage::ApplicationData::Current->LocalFolder . Example:

C:\Users\<username>\AppData\Local\Packages\<packagename>\LocalState\SubFolder1\SubFolder2\data.txt

In Windows 8.1 we have received a few reports from users that say state isn't remembered between app invocations. Upon closer inspection the files are not created (the subfolders are indeed created, but there are no files inside them)

Notes:

  1. Subfolders are created using CreateDirectory() , files are created using fopen()

  2. Files are created/opened using absolute paths

  3. This always worked under Windows 8.0 and the code has not been changed since. In fact, one of our user reports stated that the app saved files fine under Windows 8.0, but stopped saving after the user upgraded to Windows 8.1.

  4. We have not been able to replicate the issue locally using Windows 8.1. We're not sure how common this failure is, but we estimate that most users are unaffected. Affected users do not appear to have any special hardware/software configuration.

  5. If a user is affected, then files are consistently never saved, even after retrying or uninstalling and re-installing the app (ie, it's not a case of intermittent failure)

  6. It's hard to get error information given (i) the rarity of the issue (ii) the fact that the logs that would reveal this are by definition not saved, and (iii) the apps don't require internet connectivity so there is no alternative communication channel.

Can anyone think of any reason why this might fail under Windows 8.1?

Are there non-ascii characters in path to appdata? CreateDirectory has unicode version, but fopen takes const char* strings as argument.

If I were you, I'd try to abstract away from OS-specific calls using something like boost or Qt. That should work, because Qt uses unicode string for opening files and Boost should have something similar (unsure about this one).

Also on windows compiler _wfopen may be present. It is the same as fopen, but takes wchar_t strings as argument. It should work for you, but you'll need a few ifdefs here and there.

You could also try setting current directory with function that supports unicode and then calling fopen , but I wouldn't call it a "clean" solution.

Anyway, when you run into problem that is related to system calls, then on machine with a problem you can monitor calls using something like process monitor . You could instruct user with a problem to do that and send you a log.

Why don't you use the Windows.Storage classes to work with filesystem? WinRT is recommended way to work with IO not legacy C API. I believe it is more robust approach and you could get more info about the cause from WinRT exception rather than from an unknown failure of old API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM