-1

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 ==========
)
3
  • I believe this: https://godbolt.org/z/Mo4nTz says there are no errors. Perhaps your bug is caused by the previous executable still running. Or a problem with your antivirus / malware protection
    – drescherjm
    Jul 16, 2020 at 13:43
  • 1
    i just posted those message from the output tab Jul 16, 2020 at 14:58
  • 1
    I think you needed to put #define _CRT_SECURE_NO_WARNINGS above the first include. But I may be wrong.
    – drescherjm
    Jul 16, 2020 at 15:01

1 Answer 1

-1

This is because Preprocessor definition lack _CRT_SECURE_NO_WARNINGS. So, you could select Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions and add _CRT_SECURE_NO_WARNINGS.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.