fopen
Here’s the syntax:
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);
fopen opens the file named by the filenameparameter and associates a stream with it. The mode parameter specifies how the file is to be opened. It’s one of the following strings:
❑ “r”or “rb”: Open for reading only
❑ “w”or “wb”: Open for writing, truncate to zero length
❑ “a”or “ab”: Open for writing, append to end of file
❑ “r+”or “rb+”or “r+b”: Open for update (reading and writing)
❑ “w+”or “wb+”or “w+b”: Open for update, truncate to zero length
❑ “a+”or “ab+”or “a+b”: Open for update, append to end of file
The bindicates that the file is a binary file rather than a text file.
UNIX and Linux do not make a distinction between text and binary files.UNIX and Linux treat all files exactly the same, effectively as binary files. It’s also important to note that
the mode parameter must be a string, and not a character.
If successful, fopenreturns a non-null FILE *pointer. If it fails, it returns the value NULL, defined in stdio.h.
fread
The freadlibrary function is used to read data from a file stream.
Here’s the syntax:
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
fwrite
Here’s the syntax:
#include <stdio.h>
size_t fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream);
fclose
The fclose library function closes the specified stream, causing any unwritten data to be written. It’s important to use fclose because the stdio library will buffer data.#include <stdio.h>
int fclose(FILE *stream);
fflush
The fflush library function causes all outstanding data on a file stream to be written immediately.Note that an implicit flush operation is carried out whenHere’s the syntax:
#include <stdio.h>
int fflush(FILE *stream);
fseek
The fseek sets the position in the stream for the next read or write on that stream.#include <stdio.h>
int fseek(FILE *stream, long int offset, int whence);
The offset parameter is used to specify the position, and the whence parameter specifies how the offset is used. whence can be one of the following:
❑ SEEK_CUR: offset is relative to the current position
❑ SEEK_END: offset is relative to the end of the file
fgetc, getc, and getchar
The fgetc function returns the next byte, as a character, from a file stream. When it reaches the end of the file or there is an error, it returns EOF. You must use ferror or feof to distinguish the two cases.Here’s the syntax:
#include <stdio.h>
int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar();
The getc function is equivalent to fgetc, except that it may be implemented as a macro. In that case the stream argument may be evaluated more than once so it does not have side effects (for example,it shouldn’t affect variables). Also, you can’t guarantee to be able use the address of getc as a function pointer.
The getchar function is equivalent to getc(stdin) and reads the next character from the standard input.
fputc, putc, and putchar
The fputc function writes a character to an output file stream. It returns the value it has written, or EOF on failure.#include <stdio.h>
int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);
As with fgetc/getc, the function putc is equivalent to fputc, but it may be implemented as a macro.
The putchar function is equivalent to putc(c,stdout), writing a single character to the standard output.
fgets and gets
The fgets function reads a string from an input file stream.#include <stdio.h>
char *fgets(char *s, int n, FILE *stream);
char *gets(char *s);
fgets writes characters to the string pointed to by s until a newline is encountered, n-1 characters have been transferred, or the end of file is reached, whichever occurs first. Any newline encountered is transferred to the receiving string and a terminating null byte, \0, is added. Only a maximum of n-1 characters are transferred in any one call because the null byte must be added to mark the end of the string and bring the total up to n bytes.
When it successfully completes, fgets returns a pointer to the string s. If the stream is at the end of a file, it sets the EOF indicator for the stream and fgets returns a null pointer. If a read error occurs, fgets returns a null pointer and sets errno to indicate the type of error.
The gets function is similar to fgets, except that it reads from the standard input and discards any newline encountered. It adds a trailing null byte to the receiving string.