-1

我正在尝试在 Visual Studio 2019 中使用 fread 和 fwrite 将内容从源文件复制到目标文件,但我遇到以下错误:1> 完成构建项目“project.vcxproj”——失败。这是我的代码:

#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;
}

请帮我指出我的问题来自哪里。我是视觉工作室的新手。提前致谢。(PS:这是我的输出表

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 ==========
)
4

1 回答 1

0

这是因为Preprocessor definition缺乏_CRT_SECURE_NO_WARNINGS。因此,您可以选择Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions并添加_CRT_SECURE_NO_WARNINGS.

在此处输入图像描述

于 2020-07-17T06:34:04.050 回答