博客專欄

EEPW首頁 > 博客 > 嵌入式Linux:格式化I/O

嵌入式Linux:格式化I/O

發(fā)布人:美男子玩編程 時間:2024-07-29 來源:工程師 發(fā)布文章

在Linux中,格式化I/O(formatted I/O)指的是通過格式化輸入輸出函數(shù)對數(shù)據(jù)進行讀寫,這些函數(shù)允許你以特定的格式讀寫數(shù)據(jù)。


拓展:Linux實現(xiàn)標準輸入和標準輸出(STDIN_FILENO和STDOUT_FILENO)


格式化輸出函數(shù)

C 庫函數(shù)提供了 5 個格式化輸出函數(shù),包括:printf()、fprintf()、dprintf()、sprintf()、snprintf()。


1.1、printf()函數(shù)

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


功能:將格式化的字符串輸出到標準輸出(通常是終端)。


返回值:返回寫入的字符數(shù)(不包括終止的空字符),如果出錯則返回負值。


示例:








#include <stdio.h> int main() {    int age = 30;    printf("Age: %d\n", age);    return 0;}



1.2、fprintf()函數(shù)

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


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


返回值:返回寫入的字符數(shù)(不包括終止的空字符),如果出錯則返回負值。


示例:


#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()函數(shù)

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


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


返回值:返回寫入的字符數(shù)(不包括終止的空字符),如果出錯則返回負值。


示例:


#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()函數(shù)

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


功能:將格式化的字符串輸出到字符串緩沖區(qū)。


返回值:返回寫入的字符數(shù)(不包括終止的空字符),如果出錯則返回負值。


示例:


#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()函數(shù)

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


功能:將格式化的字符串輸出到字符串緩沖區(qū),最多寫入size個字符。


返回值:返回要寫入的字符數(shù),如果返回值大于等于size,則表示輸出被截斷。


示例:


#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


格式化輸入函數(shù)

C 庫函數(shù)提供了 3 個格式化輸入函數(shù),包括:scanf()、fscanf()、sscanf()。


2.1、scanf()函數(shù)

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


功能:從標準輸入讀取格式化數(shù)據(jù)。


返回值:成功匹配和賦值的輸入項數(shù),如果遇到錯誤或到達文件末尾,則返回負值。


示例:


#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()函數(shù)

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


功能:從指定的文件流讀取格式化數(shù)據(jù)。


返回值:成功匹配和賦值的輸入項數(shù),如果遇到錯誤或到達文件末尾,則返回負值。


示例:


#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()函數(shù)

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


功能:從字符串緩沖區(qū)讀取格式化數(shù)據(jù)。


返回值:成功匹配和賦值的輸入項數(shù),如果遇到錯誤或到達字符串末尾,則返回負值。


示例:


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


這些格式化輸入輸出函數(shù)提供了豐富的功能,便于處理各種類型的數(shù)據(jù)輸入輸出需求。


使用這些函數(shù)時需要特別注意格式化字符串的正確性和緩沖區(qū)的大小,以避免緩沖區(qū)溢出和其他潛在問題。


*博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點,如有侵權(quán)請聯(lián)系工作人員刪除。



關(guān)鍵詞: 嵌入式 Linux

相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉