linux 标准 I/O(Standard I/O)库提供了一系列函数,用于高级文件输入和输出操作。这些函数构建在底层文件 I/O 系统调用之上,为开发者提供了更便捷、更高级的文件处理方式。以下是一些常用的 Linux 标准 I/O 库函数:
-
文件指针和标准 I/O:标准 I/O 使用 FILE 结构来表示文件流,通过文件指针(FILE*)进行操作。FILE 结构包含文件的缓冲区、文件描述符、文件位置指示器等信息。
-
文件打开和关闭:
#include <stdio.h> int main() { // 打开文件 FILE* file = fopen("example.txt", "r"); if (file != NULL) { // 文件操作... // 关闭文件 fclose(file); } else { perror("Error opening file"); } return 0; }
- 文件读写:
- size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream); 从文件读取数据到缓冲区。ptr 是数据缓冲区的指针,size 是每个数据项的大小,nmemb 是要读取的数据项数量,stream 是文件指针。
- size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream); 将数据从缓冲区写入文件。ptr 是数据缓冲区的指针,size 是每个数据项的大小,nmemb 是要写入的数据项数量,stream 是文件指针。
#include <stdio.h> #include <string.h> int main() { FILE* file = fopen("example.txt", "w"); if (file != NULL) { // 写入数据 const char* data = "Hello, Standard I/O!"; fwrite(data, sizeof(char), strlen(data), file); // 关闭文件 fclose(file); } else { perror("Error opening file"); } return 0; }
- 标准输入输出:
#include <stdio.h> int main() { FILE* file = fopen("example.txt", "w"); if (file != NULL) { // 将格式化的数据写入文件 fprintf(file, "The answer is: %dn", 42); // 关闭文件 fclose(file); } else { perror("Error opening file"); } return 0; }
- 标准错误输出:
- int perror(const char* s); 输出一个错误消息。
#include <stdio.h> int main() { FILE* file = fopen("nonexistent_file.txt", "r"); if (file == NULL) { // 输出错误消息 perror("Error opening file"); } return 0; }
- 标准输入输出重定向:
- int fflush(FILE* stream); 刷新缓冲区。
- int setvbuf(FILE* stream, char* buffer, int mode, size_t size); 设置文件流的缓冲区。
- int fseek(FILE* stream, long offset, int whence); 移动文件位置指示器。
#include <stdio.h> int main() { FILE* file = fopen("example.txt", "w"); if (file != NULL) { // 写入数据 fprintf(file, "Hello, Standard I/O!"); // 刷新缓冲区 fflush(file); // 移动文件位置指示器 fseek(file, 0, SEEK_SET); // 关闭文件 fclose(file); } else { perror("Error opening file"); } return 0; }
-
标准输入输出缓冲:标准 I/O 库使用缓冲区来提高性能。默认情况下,标准输入(stdin)和标准输出(stdout)是行缓冲的,而标准错误输出(stderr)是无缓冲的。
- 行缓冲:当遇到换行符 n 时,缓冲区会被刷新。
- 全缓冲:当缓冲区满时,或者使用 fflush 函数时,缓冲区会被刷新。
#include <stdio.h> int main() { // 默认为行缓冲,当遇到换行符时,缓冲区被刷新 printf("Hello, "); // 不会立即输出 // 刷新缓冲区 fflush(stdout); // 输出 "Hello, " // 全缓冲,缓冲区满时被刷新 printf("World!"); // 不会立即输出 // 刷新缓冲区 fflush(stdout); // 输出 "World!" return 0; }
-
标准输入输出重定向:标准输入输出重定向允许程序从不同的源读取输入或将输出发送到不同的目标。这在命令行中特别有用。
- 标准输入重定向:./program
- 标准输出重定向:./program > output.txt 程序将输出写入到 output.txt。
#include <stdio.h> int main() { // 从标准输入读取数据 int number; scanf("%d", &number); printf("You entered: %dn", number); // 将输出重定向到文件 freopen("output.txt", "w", stdout); printf("This will be written to output.txtn"); return 0; }
-
标准输入输出流的复制:通过 dup 或 dup2 函数,可以复制文件描述符,实现标准输入输出流的重定向。
- int dup(int oldfd); 复制文件描述符,返回新的文件描述符。
- int dup2(int oldfd, int newfd); 复制文件描述符到指定的文件描述符,如果指定的文件描述符已经打开,则关闭它。
#include <stdio.h> #include <unistd.h> int main() { // 复制标准输出流到文件描述符 3 int newfd = dup(STDOUT_FILENO); // 输出到标准输出和新的文件描述符 printf("Hello, Standard Output!n"); dprintf(newfd, "Hello, New File Descriptor!n"); // 关闭新的文件描述符 close(newfd); return 0; }
- 标准错误输出:标准错误输出流 stderr 通常用于输出程序运行时的错误消息。
#include <stdio.h> int main() { fprintf(stderr, "This is an error message.n"); perror("An error occurred"); return 0; }
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END