File * fopen (const char * filename, const char * mode)
Mode of file name opened by filename
Returns a file pointer.
Size_t fread (void * ptr, size_t size, size_t, file * stream)
Reads n fields from the file pointed to by stream. Each field is size byte, and the read data is placed in the character array indicated by ptr. The actual number of bytes read is returned.
Size_t fwrite (const void * ptr, size_t size, size_t n, file * stream)
Write n fields from the array referred to by the buffer ptr to the file pointed to by stream. Each field is in size and returns the number of actually written fields.
Int fgetc (file * stream)
Reads a character from a specified file.
Example 1:
# Include <stdio. h>
# Include <stdlib. h>
Int main (void)
{
File * fp;
Char ch;
If (fp = fopen ("test.txt", "rt") = null)
{
Printf ("file open error! ");
Exit (1 );
}
Ch = fgetc (fp );
While (ch! = Eof)
{
Putchar (ch );
Ch = fgetc (fp );
}
Fclose (fp );
}
Int fputc (int c, file * stream)
Write a character to the specified file.
Example 2:
# Include <stdio. h>
# Include <stdlib. h>
Int main (void)
{
File * fp;
Char ch;
If (fp = fopen ("test.txt", "wt +") = null)
{
Printf ("file open error! ");
Exit (1 );
}
Printf ("please enter a stringn ");
Ch = getchar ();
While (ch! = 'N ')
{
Fputc (ch, fp );
Ch = getchar ();
}
Fclose (fp );
}
Fscanf (file * stream, char * format [, argument...])
Format input from a stream
# Include <stdlib. h>
# Include <stdio. h>
Int main (void)
{
Int I;
Printf ("please enter an interger :");
If (fscanf (stdin, "% d", & I) // read from the standard input stream
Printf ("the interger is: % dn", I );
Return 0;
}
Int fprintf (file * stream, char * format [, argument...])
Format the output to a stream
# Include <stdio. h>
# Include <stdlib. h>
Int main (void)
{
File * stream;
Int I = 10;
Double fp = 1.5;
Char s [] = "hello world ";
Char c = 'n ';
Stream = fopen ("fprint. out", "w ");
Fprintf (stream, "% s % c", s, c );
Fprintf (stream, "% dn", I );
Fprintf (stream, "% fn", fp );
Fclose (stream );
Return 0;
}
Int fseek (file * stream, long offset, int whence)
Set the file pointer whence:
Seek_set: search from the beginning of the file
Seek_cur: search from the current location
Seek_end: search from the end of the file