C File handling in visual studio 2017
C File handling in visual studio 2017
The file handling commands in Visual Studio seem to be different than normal. I'm currently learning the very basics of File Handling in C, but the commands don't seem to be working. This is what I've got right now -
#include <stdio.h>
int main()
int num;
FILE *fptr;
fptr = fopen("C:\", "program.txt", "w");
if (fptr == NULL)
printf("Error!");
exit(1);
printf("Enter num: ");
scanf_s("%d", &num);
fprintf(fptr, "%d", num);
fclose(fptr);
return 0;
Here's the build output-
'fopen': too many actual parameters
warning C4013: 'exit' undefined; assuming extern returning int
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.
When I use fopen_s instead, like this fopen_s("C:program.txt", "w"), it says-
'function': 'FILE **' differs in levels of indirection from 'char [15]'
'fopen_s': different types for formal and actual parameter 1
'fopen_s': too few arguments for call
'=': 'FILE *' differs in levels of indirection from 'errno_t'
I need some serious help.
exit
stdlib.h
#include <stdlib.h>
VS Developers Command Prompt
cl /W3 /wd4996 /Ox /FeNameForExe /Tc yoursource.c
yoursource.c
NameForExe.exe
2 Answers
2
You should open your file with either
FILE * f;
f= fopen("C:\program.txt", "w");
or
FILE * f;
int err = fopen_s(&f, "C:\program.txt", "w");
the latter takes FILE ** as an extra argument, and return error code (0 on success).
I'm sorry but can you explain the err bit? and the part "&f" part, to access the adress of f, shouldn't I declare it first? should I do FILE *f for it then?
– Chase
Sep 2 at 15:48
FILE *f; // this declaration is common for both examples. so yes, you have to declare it first; regarding the "inte err= fopen_s(...)", this is an API change. fopen_s returns error status, (0 on success) and it returns FILE object through the first argument (this is why you have to pass a pointer to it)
– Alexey Polyudov
Sep 2 at 19:29
There is a extra comma ,
in fopen()
which makes fopen()
as three arguments, which is wrong & causing the error
,
fopen()
fopen()
'fopen': too many actual parameters
This
fptr = fopen("C:\", "program.txt", "w"); /* fopen() expects 2 arguments */
replaces with
fptr = fopen("C:\program.txt", "w");
You can disable below
'fopen': This function or variable may be unsafe. Consider using
fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
warning by
#pragma warning(disable:4996) /* use it before other headers */
Or use fopen_s()
.
fopen_s()
Compiling from the command line
/wd4996
will disable the warning as well.– David C. Rankin
Sep 2 at 7:03
/wd4996
That's very informative & handy solution @DavidC.Rankin I didn't try it before.
– Achal
Sep 2 at 7:06
Yes, when I would compile using the
/Wall
warning level for VS from the Developers Command Prompt -- I have a standard string of /wdXXXX
warnings to disable all the unwanted non-code warnings. I finally got tired of how chatty /Wall
can be and just went with /W3
, verbose enough, but primarily code-related warnings. (/wd4996
would still be required there) You can enter as many /wdXXXX
options as you like where XXXX
is the warning number you want to suppress. cl /?
explains it well.– David C. Rankin
Sep 2 at 7:12
/Wall
/wdXXXX
/Wall
/W3
/wd4996
/wdXXXX
XXXX
cl /?
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
exit
is declared instdlib.h
, so#include <stdlib.h>
to handle that error. For small programs, I would recommend opening theVS Developers Command Prompt
and learning to compile from the command line. Much faster than using the IDE, and you also learn what the compiler options are -- so you can then tell the IDE what you want it to do. A minimum command line would becl /W3 /wd4996 /Ox /FeNameForExe /Tc yoursource.c
. Which would compileyoursource.c
and createNameForExe.exe
. No project, no solutions, no pre-compiled headers -- just C.– David C. Rankin
Sep 2 at 7:15