Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

i took the code below from MSDN and pasted in my project, But it gives me an error which says:

error C4996: '_wfopen': This function or variable may be unsafe. Consider using _wfopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Here is the code:
C++
#include "stdafx.h"//The only line I've added.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>

#define BUFFER_SIZE 50

int main(int argc, char** argv)
{
wchar_t str[BUFFER_SIZE];
size_t  strSize;
FILE*   fileHandle;

    // Create an the xml file in text and Unicode encoding mode.
    if ((fileHandle = _wfopen( L"_wfopen_test.xml",L"wt+,ccs=UNICODE")) == NULL) // C4996
    // Note: _wfopen is deprecated; consider using _wfopen_s instead
    {
        wprintf(L"_wfopen failed!\n");
        return(0);
    }

    // Write a string into the file.
    wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"<xmlTag>\n");
    strSize = wcslen(str);
    if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
    {
        wprintf(L"fwrite failed!\n");
    }

    // Write a string into the file.
    wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"</xmlTag>");
    strSize = wcslen(str);
    if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
    {
        wprintf(L"fwrite failed!\n");
    }

    // Close the file.
    if (fclose(fileHandle))
    {
        wprintf(L"fclose failed!\n");
    }
    return 0;
}


Here is the link on MSDN:


http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx[^]

Why does this error come up?
How should i fix it?
Posted
Comments
m.r.m.40 1-Aug-14 12:04pm    
Whatever it was about, i forgot it and solved my file problem with the code below:

/////////////////////////////////
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
string str1;
ofstream fp("file1.txt");
if(fp.is_open()){
cout<<"\n Enter first string ::: ";
cin>>str1;
fp<<str1<<"\n";
cout<<"the first string is written in the file.";
cout<<"\nenter second string :::";
cin>>str1;
fp<<str1;
cout<<"the second string is written in the file.";
fp.close();}
ifstream fp1("file1.txt");
cout<<"the file will be opened and read.";
while(!fp1.eof()){
fp1>>str1;
cout<<"\n"<<str1;}
fp1.close();
cout<<"\n press any key to continue :::";string s;cin>>s;
return 0;
}
////////////////////////
Sergey Alexandrovich Kryukov 1-Aug-14 12:12pm    
And why you did not follow the recommendation and did not use _wfopen_s?
—SA
m.r.m.40 1-Aug-14 13:19pm    
It was a bit hard, i'll work on that later.
Sergey Alexandrovich Kryukov 1-Aug-14 21:36pm    
This is what you should do first.
—SA
m.r.m.40 2-Aug-14 2:14am    
Ok,
I'll follow your advise.

1 solution

It's a Microsoft thing, they are potentially unsafe: your usage looks perfectly fine.
You may either (as suggested) disable the compiler deprecation or use _wfopen_s.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Aug-14 21:38pm    
This is what I advised in my commend to do first.
But I would advise not to switch off the deprecation, by all means. Anyway, my 5.
—SA
CPallini 2-Aug-14 3:45am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900