嵌入式Linux:格式化I/O

目录

1、格式化输出函数

1.1、printf()函数

1.2、fprintf()函数

1.3、dprintf()函数

1.4、sprintf()函数

1.5、snprintf()函数

2、格式化输入函数

2.1、scanf()函数

2.2、fscanf()函数

2.3、sscanf()函数


在Linux中,格式化I/O(formatted I/O)指的是通过格式化输入输出函数对数据进行读写,这些函数允许你以特定的格式读写数据。

拓展:Linux实现标准输入和标准输出(STDIN_FILENO和STDOUT_FILENO)

1、格式化输出函数

C 库函数提供了 5 个格式化输出函数,包括:printf()、fprintf()、dprintf()、sprintf()、snprintf()。

1.1、printf()函数

原型int printf(const char *format, ...);

功能:将格式化的字符串输出到标准输出(通常是终端)。

返回值:返回写入的字符数(不包括终止的空字符),如果出错则返回负值。

示例

cpp 复制代码
#include <stdio.h>

int main() {
    int age = 30;
    printf("Age: %d\n", age);
    return 0;
}

1.2、fprintf()函数

原型int fprintf(FILE *stream, const char *format, ...);

功能:将格式化的字符串输出到指定的文件流。

返回值:返回写入的字符数(不包括终止的空字符),如果出错则返回负值。

示例

cpp 复制代码
#include <stdio.h>

int main() {
    FILE *file = fopen("output.txt", "w");
    if (file != NULL) {
        fprintf(file, "Hello, file!\n");
        fclose(file);
    } else {
        perror("Failed to open file");
    }
    return 0;
}

1.3、dprintf()函数

原型int dprintf(int fd, const char *format, ...);

功能:将格式化的字符串输出到指定的文件描述符。

返回值:返回写入的字符数(不包括终止的空字符),如果出错则返回负值。

示例

cpp 复制代码
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd != -1) {
        dprintf(fd, "Hello, dprintf!\n");
        close(fd);
    } else {
        perror("Failed to open file");
    }
    return 0;
}

1.4、sprintf()函数

原型int sprintf(char *str, const char *format, ...);

功能:将格式化的字符串输出到字符串缓冲区。

返回值:返回写入的字符数(不包括终止的空字符),如果出错则返回负值。

示例

cpp 复制代码
#include <stdio.h>

int main() {
    char buffer[100];
    int age = 30;
    sprintf(buffer, "Age: %d", age);
    printf("%s\n", buffer);
    return 0;
}

1.5、snprintf()函数

原型int snprintf(char *str, size_t size, const char *format, ...);

功能 :将格式化的字符串输出到字符串缓冲区,最多写入size个字符。

返回值 :返回要写入的字符数,如果返回值大于等于size,则表示输出被截断。

示例

cpp 复制代码
#include <stdio.h>

int main() {
    char buffer[10];
    int age = 30;
    snprintf(buffer, sizeof(buffer), "Age: %d", age);
    printf("%s\n", buffer);
    return 0;
}

2、格式化输入函数

C 库函数提供了 3 个格式化输入函数,包括:scanf()、fscanf()、sscanf()。

2.1、scanf()函数

原型int scanf(const char *format, ...);

功能:从标准输入读取格式化数据。

返回值:成功匹配和赋值的输入项数,如果遇到错误或到达文件末尾,则返回负值。

示例

cpp 复制代码
#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("You entered: %d\n", age);
    return 0;
}

2.2、fscanf()函数

原型int fscanf(FILE *stream, const char *format, ...);

功能:从指定的文件流读取格式化数据。

返回值:成功匹配和赋值的输入项数,如果遇到错误或到达文件末尾,则返回负值。

示例

cpp 复制代码
#include <stdio.h>

int main() {
    FILE *file = fopen("input.txt", "r");
    if (file != NULL) {
        int age;
        fscanf(file, "%d", &age);
        printf("Age from file: %d\n", age);
        fclose(file);
    } else {
        perror("Failed to open file");
    }
    return 0;
}

2.3、sscanf()函数

原型int sscanf(const char *str, const char *format, ...);

功能:从字符串缓冲区读取格式化数据。

返回值:成功匹配和赋值的输入项数,如果遇到错误或到达字符串末尾,则返回负值。

示例

cpp 复制代码
#include <stdio.h>

int main() {
    const char *input = "30";
    int age;
    sscanf(input, "%d", &age);
    printf("Age from string: %d\n", age);
    return 0;
}

这些格式化输入输出函数提供了丰富的功能,便于处理各种类型的数据输入输出需求。

使用这些函数时需要特别注意格式化字符串的正确性和缓冲区的大小,以避免缓冲区溢出和其他潜在问题。

相关推荐
FJW02081411 分钟前
【Linux】web服务器的部署和优化
linux·运维·服务器·rhce
Linux运维老纪18 分钟前
Python文件操作及数据库交互(Python File Manipulation and Database Interaction)
linux·服务器·数据库·python·云计算·运维开发
weixin_4307509319 分钟前
智能小助手部署 Win10 + ollama的Deepseek + CentOS+ maxKB
linux·人工智能·机器学习·语言模型·自然语言处理·centos
有谁看见我的剑了?26 分钟前
docker 运行时权限和 Linux 能力了解
linux·docker·容器
平生不喜凡桃李30 分钟前
Linux 进程控制
linux·运维·服务器
hope_wisdom30 分钟前
Linux系统编程之内存映射
linux·mmap·内存映射·munmap
偶尔微微一笑1 小时前
sgpt在kali应用
linux·人工智能·python·自然语言处理
余辉zmh1 小时前
【Linux系统篇】:信号的生命周期---从触发到保存与捕捉的底层逻辑
android·java·linux
鱼与宇1 小时前
Linux常用命令
linux·运维·服务器
鱼丸丶粗面1 小时前
Python 读取 txt 文件详解 with ... open()
linux·数据库·python