0

I have a file and I want to get the size of the file. I can use only _wfopen or _wfopen_s for opening the file because my file path type is std::wstring.

FILE* p_file = NULL;
p_file=_wfopen(tempFileName.c_str(),L"r");
fseek(p_file,0,SEEK_END);

but I am getting an error

error C2220: warning treated as error - no 'object' file generated  
masoud
  • 55,379
  • 16
  • 141
  • 208
mystack
  • 4,910
  • 10
  • 44
  • 75

1 Answers1

0

To get rid of your error message, you need to fix the issue that is generating warning.

If you compile this code:

#include "stdafx.h"
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    FILE* p_file = NULL;
    std::wstring tempFileName = L"c:\\test.txt";
    p_file=_wfopen(tempFileName.c_str(),L"r");

    if(!p_file)
    {
        perror("Open failed.");
        return 0;
    }

    fseek(p_file,0,SEEK_END);
    fclose(p_file);

    return 0;
}

You will get this warning:

warning C4996: '_wfopen': This function or variable may be unsafe. Consider using _wfopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

So, listen what it says, and do the following:

#include "stdafx.h"
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    FILE* p_file = NULL;
    std::wstring tempFileName = L"c:\\test.txt";
    _wfopen_s(&p_file, tempFileName.c_str(),L"r");

    if(!p_file)
    {
        perror("Open failed.");
        return 0;
    }

    fseek(p_file,0,SEEK_END);
    fclose(p_file);

    return 0;
}

There is a way to turn off this warning by putting _CRT_SECURE_NO_WARNINGS in Project Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, but you should always prefer safe alternatives to these functions.

Also, before fseek you should check if your p_file pointer is NULL.

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91