Comparison of usage of fopen and fopen_s

Source: Internet
Author: User

comparison of usage of fopen and fopen_s

Fopen and fopen_s

fopen usage: fp = fopen (filename, "w").

Fopen_s usage:, you must define another variable errno_t err, then err = fopen_s (&fp,filename, "w").

Return value: fopen Open file succeeded, return the file pointer (assigned to FP), open failed to return null value;

Fopen_s Open File successfully returned 0, failed to return non 0.

After you define the file * FP, the usage of fopen is: fp = fopen (filename, "w"). For fopen_s, you have to define another variable errno_t err, then err = fopen_s (&fp,filename, "w"). return value, for fopen, the open file succeeds returns the file pointer (assigned to the FP), the open failure returns a null value, and for fopen_s, the open file returns 0 successfully, and the failure returns 0.

In VS programming, there are often such warnings: Warning C4996: ' fopen ': This function or variable can be unsafe. Consider using fopen_s instead. To disable deprecation, use_crt_secure_no_warnings.  See online Help for details. Because fopen_s more than fopen overflow detection, more secure. (In later articles there is a comparison between get and get_s, the comparison of strcpy strcpy_s, their common point is to use some unpredictable behavior, will be explained in detail later)

#include

FILE *stream, *stream2;

int main (void)
{
int numclosed;
errno_t err;

Open for read (would fail if file "CRT_FOPEN_S.C" does not exist)
if (err = fopen_s (&stream, "CRT_FOPEN_S.C", "R"))!=0)
printf ("The file ' crt_fopen_s.c ' is not opened\n");
Else
printf ("The file ' crt_fopen_s.c ' was opened\n");

Open for Write
if (err = fopen_s (&stream2, "data2", "w+"))! = 0)
printf ("The file ' data2 ' is not opened\n");
Else
printf ("The file ' data2 ' was opened\n");

Close Stream If it is not NULL
if (stream)
{
if (fclose (stream))
{
printf ("The file ' crt_fopen_s.c ' is not closed\n");
}
}

All other files is closed:
numclosed = _fcloseall ();
printf ("Number of files closed by _fcloseall:%u\n", numclosed);
}

Comparison of usage of fscanf and fscanf_s

FSCANF and fscanf_s

fscanf Usage: fscanf (FP, "%d", &var)

fscanf_s usage: fscanf (FP, "%d", &var,sizeof (int))

Difference: fscanf_s need to specify length

FSCANF (formatted string input)
Related functions scanf,sscanf
Table header File #include <stdio.h>
Defining functions int fscanf (FILE * stream, const char *format,....);
Function description FSCANF () reads the string from the file stream of the parameter stream, and then transforms and formats the data according to the parameter format string. Format conversion form please refer to scanf (). The converted structure is stored within the corresponding parameters.
return value Success returns the number of arguments, failure returns 1, and the reason for the error is stored in errno.
Additional Instructions
Example #include <stdio.h>
Main ()
{
int i;
unsigned int J;
Char s[5];
FSCANF (stdin, "%d%x%5[a-z]%*s%f", &i,&j,s,s);
printf ("%d%d%s \ n", i,j,s);
}
Perform Ten 0x1b aaaaaaaaa bbbbbbbbbb
Ten AAAAA

fscanf function:

FSCANF (FP, "%s", Temp_str), and fscanf (FP, "%lf", &min_snr);

FSCANF is to read data from a file and save it to a variable that starts with the third argument, and the FP is a pointer to the type file.  FSCANF (FP, "%s", TEMP_STR); is to read a string from the file pointer fp, save to Temp_str inside, and scanf almost, just scanf is from the keyboard input, fscanf is read from the file fscanf (FP, "%lf", &min_snr);

Comparison of usage of fopen and fopen_s

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.