【问题标题】:C Code to Write a File will Not Compile [duplicate]编写文件的C代码将无法编译[重复]
【发布时间】:2018-01-28 17:14:28
【问题描述】:

我正在制作一个程序,它可以读取字符作为输入,然后将它们输出到用户指定的文件中。我认为它正在工作,但现在它不会编译。代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    int c = 0;
    char FileName[100] = "";
    FILE *fp;


    printf("Please input the title of the file you're interested in: ");
    scanf("%100s", FileName);

    fp = fopen(FileName, "w");

    if (fp == NULL)
    {
        perror(NULL);
        exit(0);
    }

    printf("Please input what you want in your file \n(Press Ctrl+Shift+A to 
            end the program):");

    for (c = getchar(); c != 1; c = getchar())
    {
        if (c == '\n')
            printf("");
            fputc(c, fp);
    }

    if (c == 1);
    {
        printf("\nCtrl+Shift+A is a correct ending.\n\n");
    }

    fclose(fp);
}

在我试图找出问题的过程中,我发现问题出现在 FILE *fp 和 fopen 上。当对文件的引用被注释掉时,代码至少可以编译。我的输出告诉我使用 fopen_s(),但我不确定该函数是如何工作的,或者我当前的代码是否可以使用它,因为它不适用于 fopen()。如果有帮助,我正在使用 Microsoft Visual Studio。我没有收到错误消息,但这是输出中的内容:

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>Exercise2.c
1>c:\users\djmcn\documents\visual studio 2017\projects\1.9\exercise2.c(18): 
  error C4996: 'scanf': This function or variable may be unsafe. Consider 
  using scanf_s instead. To disable deprecation, use 
  _CRT_SECURE_NO_WARNINGS. See online help for details.
1>c:\program files (x86)\windows 
  kits\10\include\10.0.16299.0\ucrt\stdio.h(1272): note: see declaration of 
  'scanf'
1>c:\users\djmcn\documents\visual studio 2017\projects\1.9\exercise2.c(20): 
  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:\program files (x86)\windows 
  kits\10\include\10.0.16299.0\ucrt\stdio.h(207): note: see declaration of 
  'fopen'
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

【问题讨论】:

  • 您还应该发布您收到的错误消息。
  • 我很抱歉@AndrewHenle,我第一次在这里发帖我应该更清楚。

标签: c visual-studio file-handling


【解决方案1】:

一般来说,我建议有经验的工程师听从编译器的建议 - scanf 是不安全的,因为即使是最聪明的人也可能错误地使用这些较旧的 C 运行时函数创建安全漏洞(缓冲区溢出)。

但由于您刚刚开始学习,我将向您展示如何指示 Visual Studio 编译器不要在这些类型的问题上出错。

在 Visual Studio 的解决方案资源管理器中,右键单击项目标题,然后从出现的上下文菜单中选择“属性”。从项目的属性页中,导航到 C/C++ 设置并在列表中找到“预处理器”选项。点击那个。从那里,有一行用于定义预处理器定义。在您的情况下,添加“_CRT_SECURE_NO_WARNINGS”(带有分号分隔符)。

更简单的方法是在源文件的顶部添加这一行:

#define _CRT_SECURE_NO_WARNINGS

【讨论】:

  • 谢谢@selbie!我试图从列为我的副本的问题中执行#define _CRT_SECURE_NO_WARNINGS,但它仍然显示警告。也许是因为新的视觉工作室更新?您展示的另一种方法仍然有效。很抱歉没有使用 fopen_s() 和 scanf_s()。我正在学习的课程只向我们展示了 fopen() 和 scanf(),所以我不知道我应该使用它。我会尝试更改代码以使用建议的功能,但如果它会使它更安全。
【解决方案2】:

这是 Visual Studio 中的 Microsoft 编译器所独有的。此代码将使用 GCC 编译。

您可以关闭警告、使用不同的编译器或使用 scanf_s 和 fopen_s。如果您致力于 VS,请使用他们的 scanf_s 和 fopen_s 标准。

这里是另一个 StackOverflow 答案的链接,关于 scanf_s 和 scanf 之间的区别:Difference between scanf and scanf_s

这是关于 fopen_s 和 fopen 之间区别的 StackOverflow 答案:fopen / fopen_s and writing to files

您可以在此处找到 scanf_s 的文档:https://msdn.microsoft.com/en-us/library/w40768et.aspx

fopen_s 在这里: https://msdn.microsoft.com/en-us/library/z5hh6ee9.aspx

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2015-07-23
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多