I am trying to copy content from source file to target file using fread and fwrite in visual studio 2019 but I am encountering the following error: 1>Done building project "project.vcxproj" -- FAILED. Here is my code:
#include <iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
using namespace std;
int my_pc(const char* src_file, const char* dest_file) {
FILE *in_file, *out_file;
in_file = fopen(src_file, "rb");
if (in_file == NULL) {
return 1;
}
out_file = fopen(dest_file, "wb");
if (out_file == NULL) {
return 2;
}
char rec[256];
size_t bytes_in, bytes_out;
while ((bytes_in=fread(rec, 1, 256, in_file)) > 0) {
bytes_out = fwrite(rec, 1, bytes_in, out_file);
if(bytes_in!=bytes_out)
{
return 3;
}
}
fclose(in_file);
fclose(out_file);
return 0;
}
int main() {
int result;
if ( result = my_pc("c:\\temp\\1.txt", "\\temp\\2.txt") != 0) {
switch (result) {
case 1:
cout << "error from opening source file\n" << endl;
break;
case 2:
cout << "error from opening target file \n " << endl;
break;
case 3:
cout << "error from copy file \n" << endl;
break;
default:
cout << "unknown error occured\n" << endl;
break;
}
}
cout << "ok!" << endl;
}
Please help me point out where my problem comes from. I am new in visual studio. Thanks in advance. (PS: this is my output tabe
1>------ Build started: Project: Error, Configuration: Debug Win32 ------
1>Source.cpp
1>C:\Users\beiji\source\repos\Error\Error\Source.cpp(7,12): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>C:\Users\beiji\source\repos\Error\Error\Source.cpp(11,13): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>Done building project "Error.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
)
This is because
Preprocessor definition
lack_CRT_SECURE_NO_WARNINGS
. So, you could selectProperties -> C/C++ -> Preprocessor -> Preprocessor Definitions
and add_CRT_SECURE_NO_WARNINGS
.