-1

I am working on making a program that reads characters as input and then outputs them to a file specified by the user. I thought that it's working but now it will not compile. Here's the code:

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

In my attempts to figure out the problem, I've found that the trouble arises at FILE *fp and fopen. When references to files are commented out, the code at least compiles. My output is telling me to use fopen_s() but I'm not sure how that function works or if my current code would work with it given that it won't work for fopen(). I am using Microsoft Visual Studio if that helps. I don't get an error message, but here's what comes into the output:

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 ==========
Yunnosch
  • 21,438
  • 7
  • 35
  • 44
strwars
  • 15
  • 6

2 Answers2

0

This is unique to the Microsoft Compiler in Visual Studio. This code will compile using GCC.

You can either turn the warnings off, use a different compiler, or use scanf_s and fopen_s. If you are commited to VS, use their standard of scanf_s and fopen_s.

Here's a link to another StackOverflow answer about the difference between scanf_s and scanf: Difference between scanf and scanf_s

Here's a StackOverflow answer about the difference between fopen_s and fopen: fopen / fopen_s and writing to files

You can find the documentation for scanf_s here: https://msdn.microsoft.com/en-us/library/w40768et.aspx

fopen_s here: https://msdn.microsoft.com/en-us/library/z5hh6ee9.aspx

Good luck.

Maniac
  • 36
  • 4
0

Generally, I'd advise experienced engineers to follow the advice of the compiler - scanf is unsafe because even the smartest of folks can mistakenly create a security hole (buffer overrun) with of the these older C runtime functions.

But since you are just starting out and learning, I'll show you how to instruct the Visual Studio compiler to not error out on these types of issues.

From the Solution Explorer in Visual Studio, right-click on the Project title, and select "Properties" from the context-menu that appears. From the Property Pages for your project, navigate to the C/C++ settings and find the "Preprocessor" option in the list. Click that. From there, there is a row for defining the Preprocessor Definitions. In your case, add "_CRT_SECURE_NO_WARNINGS" (with a semicolon delimiter).

An even easier approach is to just add this line at the top of your source file:

#define _CRT_SECURE_NO_WARNINGS
selbie
  • 82,148
  • 13
  • 83
  • 154
  • Thank you @selbie ! I was trying to do #define _CRT_SECURE_NO_WARNINGS from the question listed as a duplicate of mine, but it still showed the warnings. Maybe it is because of the new visual studio update? The other way of doing it that you showed still works though. Apologies for not using fopen_s() and scanf_s(). The course I'm working on only showed us fopen() and scanf(), so I didn't know I should use it. I'll try to change the code to use the suggested functions though if it'll make it safer. – strwars Jan 28 '18 at 19:37