none
Can't open file for writing RRS feed

  • Question

  • I have the following code that doesn't work, as it returns zero:

    static FILE* testfile;
    testfile = _wfsopen("testing.dat", L"w", _SH_DENYNO);

    The program runs from the Debug directory, and I have checked that it has writing permission for SYSTEM. I'm using Windows 10.

    Thanks in advance.



    • Edited by Gurunama Thursday, September 5, 2019 7:10 AM Missing information
    Thursday, September 5, 2019 7:08 AM

Answers

All replies

  • I have the following code that doesn't work, as it returns zero:

    static FILE* testfile;
    testfile = _wfsopen("testing.dat", L"w", _SH_DENYNO);

    The program runs from the Debug directory, and I have checked that it has writing permission for SYSTEM. I'm using Windows 10.

    Thanks in advance.



    Hello,

    the compiler must have given you a warning or an error. You use normal char* and a wchar_t* as argument. If you have UNICODE, you should open L"testing.dat". If not, you should open mode "w" and use _fsopen.

    https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fsopen-wfsopen?view=vs-2019

    Regards, Guido


    Thursday, September 5, 2019 7:14 AM
  • Maybe the first argument is incorrect in your real program?

    Thursday, September 5, 2019 7:15 AM
  • I'm using wide characters. Not sure if that means the same as UNICODE. Anyway, I changed the code to the following:

    testfile = _wfsopen(L"testing.dat", L"w", _SH_DENYNO);

    And now it works. So the "L" in front of the filename was all that was missing. Thanks a lot.

    Regards


    • Edited by Gurunama Thursday, September 5, 2019 11:13 AM
    Thursday, September 5, 2019 11:13 AM
  • However, now I also have problems with the following:

    static FILE * testfile; patherror = SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, SHGFP_TYPE_CURRENT, userpath); error = PathAppend(userpath, TEXT("testdir\\gnsh.dat")); hFile = CreateFile(userpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); testfile = _wfsopen(userpath, L"r", _SH_DENYNO);

    Also this returns zero, and the file exist.


    • Edited by Gurunama Thursday, September 5, 2019 11:45 AM Missing information
    Thursday, September 5, 2019 11:42 AM
  • However, now I also have problems with the following:

    static FILE * testfile; patherror = SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, SHGFP_TYPE_CURRENT, userpath); error = PathAppend(userpath, TEXT("testdir\\gnsh.dat")); hFile = CreateFile(userpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); testfile = _wfsopen(userpath, L"r", _SH_DENYNO);

    Also this returns zero.

    We cannot tell from this whether you are building for unicode or mbcs and you have not shown use how userpath is declared.

    Try using _tfsopen instead of _wfsopen.

    Thursday, September 5, 2019 11:47 AM
  • Using _tfsopen didn't work. Userpath is declared like this:

    static TCHAR userpath[MAX_PATH];

    Thursday, September 5, 2019 12:02 PM
  • Using _tfsopen didn't work. Userpath is declared like this:

    static TCHAR userpath[MAX_PATH];

    We still don't know if you are building for unicode or mbcs

    In any event, you should determine the reason why the call failed by checking the error code.  Right now, we don't know what the problem is.

    Thursday, September 5, 2019 12:06 PM
  • How can I know wether I am building for unicode or mbcs? Actually, I don't know the difference. The program compiles without errors or warnings. As I understand, there is a way to pick up some error information for the opening of the file, which I read about here: https://docs.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation?view=vs-2019

    But I have difficulty understanding it. Is it possibly that you could give a short explanation how to do this?

    Sincerely

    Thursday, September 5, 2019 12:27 PM
  • Your project properties specify the character set that your project will build with.  You can see this setting by examining your project's poperty sheet.  For example -

    This shows the project is building for unicode.

    As far as errors go, you can check the error code as follows -

    FILE *f = _tfsopen(userpath, TEXT("r"), _SH_DENYNO);
    
    if (f == NULL)
    {
    	TCHAR szerror[128];
    
    	_tprintf_s(TEXT("error code is %d\n"), errno);
    	_tcserror_s(szerror, errno);
    	_tprintf_s(TEXT("error explanation is %s\n"), szerror);
    }
    

    Thursday, September 5, 2019 12:43 PM
  • It turns out that I have misintepreted the Local variable values when debugging. The Locals window says

    Name Value Type
    licensefile 0x00cbfd80 {_Placeholder=0x00000000 } _iobuf *
    _Placeholder 0x00000000 void *

    So, I misinterpreted it as having the value of zero. Sorry about that. It all now seems to be working as it should. Thanks a lot for help.

    Regards

    Thursday, September 5, 2019 1:06 PM