locked
fprintf for C++ in Visual Studio Express 2012 for Windows 8

    Question

  • I am trying to use fprintf in a C++ app in Visual Studio Express 2012.  My problem is that I can't open the file.  The relevant code is as follows:

    FILE** fout=new FILE*();

    if(fopen_s(fout,"junk.txt","w")==0)

    fprintf_s(*fout,"hello C++\n");

    else

    rootFrame->Content="open failed";

    Tuesday, July 16, 2013 3:50 PM

Answers

  • Assuming you are writing a Windows Store app and not a Win32 desktop application, the issue is that you don't have write access to the 'current working directory'. You need to specify a file location you have permission to write files to. You can confirm this by looking at the actual return value that comes back from fopen_s. See MSDN for a list of code meanings.

    Also for Windows Store apps you should be using the Unicode version not the ASCII version of fopen_s.

    For Windows Store apps use WinRT so to get a valid write location for your application you'd use:

    WCHAR path[MAX_PATH];
    auto folder = Windows::Storage::ApplicationData::Current->LocalFolder;
    wcscpy_s( path, MAX_PATH, folder->Path->Data() );
    wcscat_s( path, L"junk.txt" );
    if (_wfopen_s( fout, path, L"w" ) == 0)

    See File access and permissions in Windows Store apps

    Tuesday, July 16, 2013 6:19 PM