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

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

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

相关推荐
Flynt12 分钟前
自托管重开实录:照 HN 419 分热帖让 AI 从零搭一遍,15 分钟踩了三个真坑
linux·开源·ai编程
EmbeddedChen2 小时前
Linux 怎么查看 IP 地址和网络状态?常用网络命令入门
linux·网络·ip·route·ping
爱和冰阔落3 小时前
【Linux网络】GET 和 POST 真的只是参数位置不同吗?从 HTTP 请求方法、抓包分析到长连接与路由实战
linux·c++·http·网络编程·get与post
无足鸟ICT3 小时前
【RHCA+】_proc_pid_attr_exec
linux·运维·服务器
牙牙7053 小时前
接口一直超时,后端却查不出问题:TIME_WAIT 把本地端口耗尽了
linux·网络·tcp·连接池·故障排查·time_wait
智商偏低4 小时前
创建的 conda 环境如何迁移到领一个离线电脑上,后面随着开发需要往离线电脑上拷贝包
linux·人工智能·conda
Johny_Zhao15 小时前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
FlightYe17 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
by2099918 小时前
Linux 文件描述符与重定向
linux·服务器
橙橙笔记19 小时前
Linux基础与命令的学习
linux·运维·服务器·shell