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 NumberVariables & Description
1size_t
This is an unsigned integer type, which issizeofResult of the keyword, indicating the size of an object.
2FILE
File stream type, an object type suitable for storing file stream information.
3fpos_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 NumberMacros & Description
1NULL
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.
3BUFSIZ
This macro is an integer that representssetbufThe buffer size used by the function.
4EOF
This macro is a negative integer indicating that the end of the file has been reached.
5FOPEN_MAX
This macro is an integer that represents the number of files that the system can open simultaneously.
6FILENAME_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.
7L_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.
8SEEK_CUR、SEEK_ENDandSEEK_SET
These macros are infseekused in functions to position at different locations within a file.
9TMP_MAX
This macro is the maximum number of unique filenames that the tmpnam function can generate.
10stderr、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 NumberFunctions & Description
1int fclose(FILE *stream)
Closes the stream stream. Flushes all buffers.
2void clearerr(FILE *stream)
Clears the end-of-file and error indicators for the given stream stream.
3int feof(FILE *stream)
Tests the end-of-file indicator for the given stream stream.
4int ferror(FILE *stream)
Tests the error indicator for the given stream stream.
5int fflush(FILE *stream)
Flushes the output buffer of the stream stream.
6int fgetpos(FILE *stream, fpos_t *pos)
Gets the current file position of the stream stream and writes it to pos.
7FILE *fopen(const char *filename, const char *mode)
Opens the file pointed to by filename using the given mode mode.
8size_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.
9FILE *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.
10int 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.
11int 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.
12long int ftell(FILE *stream)
Returns the current file position of the given stream stream.
13size_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.
14int remove(const char *filename)
Deletes the given filename filename so that it can no longer be accessed.
15int rename(const char *old_filename, const char *new_filename)
Change the filename pointed to by old_filename to new_filename.
16void rewind(FILE *stream)
Sets the file position to the beginning of the file of the given stream stream.
17void setbuf(FILE *stream, char *buffer)
Defines how the stream stream should be buffered.
18int setvbuf(FILE *stream, char *buffer, int mode, size_t size)
Another function that defines how the stream stream should be buffered.
19FILE *tmpfile(void)
Create a temporary file in binary update mode (wb+).
20char *tmpnam(char *str)
Generates and returns a valid temporary filename that did not exist previously.
21int fprintf(FILE *stream, const char *format, ...)
Sends formatted output to the stream stream.
22int printf(const char *format, ...)
Sends formatted output to standard output stdout.
23int sprintf(char *str, const char *format, ...)
Send formatted output to a string.
24int vfprintf(FILE *stream, const char *format, va_list arg)
Sends formatted output to the stream stream using an argument list.
25int vprintf(const char *format, va_list arg)
Sends formatted output to standard output stdout using an argument list.
26int vsprintf(char *str, const char *format, va_list arg)
Sends formatted output to a string using an argument list.
27int fscanf(FILE *stream, const char *format, ...)
Reads formatted input from the stream stream.
28int scanf(const char *format, ...)
Reads formatted input from standard input stdin.
29int sscanf(const char *str, const char *format, ...)
Read formatted input from a string.
30int fgetc(FILE *stream)
Gets the next character (an unsigned character) from the specified stream stream and moves the position indicator forward.
31char *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.
32int 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.
33int fputs(const char *str, FILE *stream)
Writes the string to the specified stream stream, excluding the null character.
34int getc(FILE *stream)
Gets the next character (an unsigned character) from the specified stream stream and moves the position indicator forward.
35int getchar(void)
Gets a character (an unsigned character) from standard input stdin.
36char *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.
37int 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.
38int putchar(int char)
Writes the character (an unsigned character) specified by the argument char to standard output stdout.
39int 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.
40int 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.
41void 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.
42int 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

#include <stdio.h>

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

#include <stdio.h>

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;
}
Write file content:

Example

#include <stdio.h>

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

  • UsagefopenAfter opening a file, be sure to usefcloseCloses the file to avoid file resource leaks.
  • UsagefgetsWhen reading strings, be sure to pay attention to the size of the buffer to avoid buffer overflow.
  • Usageprintf、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.

other extensions