C Standard Library -<stdio.h>
Introduction
stdio .hThe header file defines three variable types, a number of macros, and various functions for performing input and output.
<stdio.h>It is a header file in the C standard library that defines various functions and types for handling files and standard input/output streams.
Library variables
The following are the variable types defined in the header file stdio.h:
| Serial Number | Variables & Description |
|---|---|
| 1 | size_t This is an unsigned integer type, which issizeofResult of the keyword, indicating the size of an object. |
| 2 | FILE File stream type, an object type suitable for storing file stream information. |
| 3 | fpos_t File position type, an object type suitable for storing any position within a file. |
Library macros
The following are the macros defined in the header file stdio.h:
| Serial Number | Macros & Description |
|---|---|
| 1 | NULL This macro is the value of a null pointer constant. |
| 2 | _IOFBF、_IOLBFand _IONBF These macros expand to integer constant expressions with specific values and are suitable forsetvbufThe third parameter of the function. |
| 3 | BUFSIZ This macro is an integer that representssetbufThe buffer size used by the function. |
| 4 | EOF This macro is a negative integer indicating that the end of the file has been reached. |
| 5 | FOPEN_MAX This macro is an integer that represents the number of files that the system can open simultaneously. |
| 6 | FILENAME_MAX This macro is an integer that represents the maximum length of a filename that a character array can store. If the implementation has no restrictions, this value should be the recommended maximum. |
| 7 | L_tmpnam This macro is an integer that represents the maximum length of a character array that can store a temporary filename created by the tmpnam function. |
| 8 | SEEK_CUR、SEEK_ENDandSEEK_SET These macros are infseekused in functions to position at different locations within a file. |
| 9 | TMP_MAX This macro is the maximum number of unique filenames that the tmpnam function can generate. |
| 10 | stderr、stdinandstdout These macros are pointers to the FILE type, corresponding to the standard error, standard input, and standard output streams, respectively. |
Library functions
The following are the functions defined in the header file stdio.h:
To better understand the functions, study them in the following sequence, because the file created in the first function will be used in subsequent functions.
| Serial Number | Functions & Description |
|---|---|
| 1 | int fclose(FILE *stream) Closes the stream stream. Flushes all buffers. |
| 2 | void clearerr(FILE *stream) Clears the end-of-file and error indicators for the given stream stream. |
| 3 | int feof(FILE *stream) Tests the end-of-file indicator for the given stream stream. |
| 4 | int ferror(FILE *stream) Tests the error indicator for the given stream stream. |
| 5 | int fflush(FILE *stream) Flushes the output buffer of the stream stream. |
| 6 | int fgetpos(FILE *stream, fpos_t *pos) Gets the current file position of the stream stream and writes it to pos. |
| 7 | FILE *fopen(const char *filename, const char *mode) Opens the file pointed to by filename using the given mode mode. |
| 8 | size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) Reads data from the given stream stream into the array pointed to by ptr. |
| 9 | FILE *freopen(const char *filename, const char *mode, FILE *stream) Associates a new filename filename with the given open stream stream, while closing the old file in the stream. |
| 10 | int fseek(FILE *stream, long int offset, int whence) Sets the file position of the stream stream to the given offset offset, parameteroffsetMeans from a givenwhenceThe number of bytes for position lookup. |
| 11 | int fsetpos(FILE *stream, const fpos_t *pos) Sets the file position of the given stream stream to the given position. The parameterposis the position given by the function fgetpos. |
| 12 | long int ftell(FILE *stream) Returns the current file position of the given stream stream. |
| 13 | size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) Writes data from the array pointed to by ptr to the given stream stream. |
| 14 | int remove(const char *filename) Deletes the given filename filename so that it can no longer be accessed. |
| 15 | int rename(const char *old_filename, const char *new_filename) Change the filename pointed to by old_filename to new_filename. |
| 16 | void rewind(FILE *stream) Sets the file position to the beginning of the file of the given stream stream. |
| 17 | void setbuf(FILE *stream, char *buffer) Defines how the stream stream should be buffered. |
| 18 | int setvbuf(FILE *stream, char *buffer, int mode, size_t size) Another function that defines how the stream stream should be buffered. |
| 19 | FILE *tmpfile(void) Create a temporary file in binary update mode (wb+). |
| 20 | char *tmpnam(char *str) Generates and returns a valid temporary filename that did not exist previously. |
| 21 | int fprintf(FILE *stream, const char *format, ...) Sends formatted output to the stream stream. |
| 22 | int printf(const char *format, ...) Sends formatted output to standard output stdout. |
| 23 | int sprintf(char *str, const char *format, ...) Send formatted output to a string. |
| 24 | int vfprintf(FILE *stream, const char *format, va_list arg) Sends formatted output to the stream stream using an argument list. |
| 25 | int vprintf(const char *format, va_list arg) Sends formatted output to standard output stdout using an argument list. |
| 26 | int vsprintf(char *str, const char *format, va_list arg) Sends formatted output to a string using an argument list. |
| 27 | int fscanf(FILE *stream, const char *format, ...) Reads formatted input from the stream stream. |
| 28 | int scanf(const char *format, ...) Reads formatted input from standard input stdin. |
| 29 | int sscanf(const char *str, const char *format, ...) Read formatted input from a string. |
| 30 | int fgetc(FILE *stream) Gets the next character (an unsigned character) from the specified stream stream and moves the position indicator forward. |
| 31 | char *fgets(char *str, int n, FILE *stream) Reads a line from the specified stream stream and stores it in the string pointed to by str. When(n-1)characters are read, or when a newline character is read, or when the end of the file is reached, it stops, whichever comes first. |
| 32 | int fputc(int char, FILE *stream) Writes the character (an unsigned character) specified by the argument char to the specified stream stream and moves the position indicator forward. |
| 33 | int fputs(const char *str, FILE *stream) Writes the string to the specified stream stream, excluding the null character. |
| 34 | int getc(FILE *stream) Gets the next character (an unsigned character) from the specified stream stream and moves the position indicator forward. |
| 35 | int getchar(void) Gets a character (an unsigned character) from standard input stdin. |
| 36 | char *gets(char *str) Reads a line from standard input stdin and stores it in the string pointed to by str. It stops when a newline character is read or when the end of the file is reached, depending on the situation. |
| 37 | int putc(int char, FILE *stream) Writes the character (an unsigned character) specified by the argument char to the specified stream stream and moves the position indicator forward. |
| 38 | int putchar(int char) Writes the character (an unsigned character) specified by the argument char to standard output stdout. |
| 39 | int puts(const char *str) Writes a string to standard output stdout, up to but not including the null character. A newline character is appended to the output. |
| 40 | int ungetc(int char, FILE *stream) Pushes the character char (an unsigned character) back onto the specified stream stream so that it is the next character to be read. |
| 41 | void perror(const char *str) Outputs a descriptive error message to standard error stderr. First the string str is output, followed by a colon, then a space. |
| 42 | int snprintf(char *str, size_t size, const char *format, ...) Format string into str. |
Example
The following are some usage<stdio.h>Examples of functions in:
Opening and closing files:
Example
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
// Perform file operations
fclose(file);
return 0;
}
Read file contents:
Example
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
char buffer[256];
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
Example
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
perror("Error opening file");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
return 0;
}
Notes
- Usage
fopenAfter opening a file, be sure to usefcloseCloses the file to avoid file resource leaks. - Usage
fgetsWhen reading strings, be sure to pay attention to the size of the buffer to avoid buffer overflow. - Usage
printf、scanfWhen using functions, be sure to pay attention to the correctness of the format string to avoid undefined behavior.
By understanding and using<stdio.h>The functions provided make it convenient to perform file and standard input/output operations, and to write more robust and flexible C programs.