嵌入式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;
}

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

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

相关推荐
HLC++8 小时前
Linux的进程间通信
android·linux·服务器
华清远见IT开放实验室9 小时前
实验室建设案例 | 石家庄科技信息职业学院嵌入式实验室——从底层硬件到系统应用,一所应用型高校的嵌入式人才培养这样落地
linux·arm开发·stm32·嵌入式硬件·高校·实验室建设
groundhappy12 小时前
idalib安装和codex ida-mcp配置
linux·开发语言·python
通信小小昕12 小时前
Ubuntu 26.04 中文输入法安装
linux·运维·ubuntu
张小姐的猫13 小时前
【Linux】网络编程 —— HTTP协议(上)
linux·运维·服务器·网络·http·单例模式·策略模式
栩栩云生14 小时前
AI 写代码犯的错,早被写进了错题集
linux·安全·ai编程
imc.1115 小时前
linux基础IO
linux·运维·服务器
BelongPanda17 小时前
Linux Nginx 纯手动 Let‘s Encrypt 泛域名证书配置教程
linux·nginx
酷可达拉斯17 小时前
Linux操作系统-shell编程(0)
linux·运维·服务器·python·云计算
2301_7779983417 小时前
Linux中断机制:操作系统如何高效运行
linux·运维·服务器