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

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

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

相关推荐
小黑爱编程23 分钟前
【LInux】HTTPS是如何实现安全传输的
linux·安全·https
BeyondESH28 分钟前
Linux线程同步—竞态条件和互斥锁(C语言)
linux·服务器·c++
鱼饼6号44 分钟前
Prometheus 上手指南
linux·运维·centos·prometheus
Asher Gu1 小时前
Linux系统编程入门 | 模拟实现 ls -l 命令
linux
fanged1 小时前
裸机编一个Hello World!(TODO)
嵌入式
c无序1 小时前
【Linux进程控制】进程程序替换
linux
小安运维日记3 小时前
Linux云计算 |【第四阶段】NOSQL-DAY1
linux·运维·redis·sql·云计算·nosql
CoolTiger、6 小时前
【Vmware16安装教程】
linux·虚拟机·vmware16
学习3人组7 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos
厨 神8 小时前
vmware中的ubuntu系统扩容分区
linux·运维·ubuntu