1:fprintf ()
#include <stdio.h>
int fprintf (FILE *stream, const char *format, ...);
The fprintf () function sends information (parameters) according to the specified format (format) to the file specified by the stream (stream). Therefore fprintf () enables the output of the information to the specified file.
Char name[20] = "Mary";
FILE *out;
out = fopen ("Output.txt", "w");
if (out!= NULL)
fprintf (out, "Hello%s\n", name);
The output format parameters are the same as printf ().
fprintf () works like printf (). The return value of fprintf () is the number of characters in the output and a negative value when an error occurs.
in some places, there is this definition: printf (...) =fprintf (stdout,...).
2:EG)
The use of the fprintf function!2007-12-13 21:46
fprintf is for file operations , the prototype is int fprintf (file *stream, const char *format [, argument] ...);
Example usage:
#include <stdio.h>
#include <process.h>
FILE *stream;
void Main (void)
{
int i = 10;
Double fp = 1.5;
Char s[] = "This is a string";
char c = ' \ n ';
stream = fopen ("Fprintf.out", "w");
fprintf (Stream, "%s%c", S, c);
fprintf (Stream, "%d\n", I);
fprintf (Stream, "%f\n", FP);
Fclose (stream);
System ("type Fprintf.out");
}
Screen output:
This is a string
10
1.500000
printf is printing a string of strings on the screen .
The prototype is int printf (const char *format [, argument] ...);
is standard output.
The usage distinction between 3:printf, sprintf and fprintf
1.PRINTF is associated with the standard output file (stdout) , fprintf does not have this limitation.
2.fprintf is for file operation , the prototype is int fprintf (file *stream, const char *format [, argument] ...);
3.SPRINTF is formatted to output to a string , fprintf is formatted to output to a stream, usually to a file .
int fprintf (FILE *stream, const char *format [, argument] ...);
int sprintf (char *buffer, const char *format [, argument] ...);
* Standard input/output stream
#include <stdio.h>
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
stderr, like Stdin,stdout, is a stream.
The specific is:
STDIN is the standard input stream, which defaults to the keyboard,
StdOut is the standard output stream, which defaults to the screen,
STDERR is the standard error stream, which generally sets the screen to default, or it can be exported to a file.
Stdin,stdout can be redirected to a file, stderr output to the screen;
So if the output error message, do not use printf ("error"), fprintf (stdout, "error");
This is because the message may be redirected to a file;
To use fprintf (stderr, "error");
bitcount function to calculate the number of binary 1 of a few examples Bitcount (5) ==2 52 is 101, there are two 1