C 庫 - fscanf() 函式



C 庫函式 int fscanf(FILE *stream, const char *format, ...) 從流中讀取格式化輸入。它是標準 I/O 庫的一部分,並在 <stdio.h> 標頭檔案中定義。此函式允許根據指定的格式提取和解析檔案中的資料。

語法

以下是 C 庫函式 fscanf() 的語法:

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

引數

此函式使用各種引數來處理檔案 -

  • *FILE stream : 指向 FILE 物件的指標,該物件標識要從中讀取資料的輸入流。
  • *const char format:包含格式字串的 C 字串,格式字串由轉換說明符組成。這些說明符確定要讀取的資料的型別和格式。
  • ... (省略號):表示可變數量的其他引數,這些引數指向將儲存解析值的地址。每個引數都必須與格式字串中的一個格式說明符對應。
序號 引數及描述
1

*

可選的起始星號表示資料要從流中讀取但要忽略,即它不會儲存在相應的引數中。

2

寬度

指定當前讀取操作中要讀取的最大字元數。

3

修飾符

指定與對應附加引數指向的資料不同的 int 大小(在 d、i 和 n 的情況下)、unsigned int 大小(在 o、u 和 x 的情況下)或 float 大小(在 e、f 和 g 的情況下):h:short int(對於 d、i 和 n),或 unsigned short int(對於 o、u 和 x)l:long int(對於 d、i 和 n),或 unsigned long int(對於 o、u 和 x),或 double(對於 e、f 和 g)L:long double(對於 e、f 和 g)

4

型別

指定要讀取的資料型別以及預期讀取方式的字元。請參閱下表。

型別 限定輸入 引數型別
c 單個字元:讀取下一個字元。如果指定了與 1 不同的寬度,則該函式將讀取寬度個字元並將它們儲存在作為引數傳遞的陣列的連續位置中。末尾不附加空字元。 char *
d 十進位制整數:前面可選帶 + 或 - 號的數字 int *
e、E、f、g、G 浮點數:包含小數點的十進位制數,前面可選帶 + 或 - 號,後面可選帶 e 或 E 字元和十進位制數。兩個有效條目的示例是 -732.103 和 7.12e4 float *
o 八進位制整數 int *
s 字元字串。這將讀取後續字元,直到找到空格(空格字元被認為是空格、換行符和製表符)。 char *
u 無符號十進位制整數。 unsigned int *
x、X 十六進位制整數 int *

返回值

fscanf 函式返回一個整數,表示成功匹配和賦值的輸入項數。如果輸入在第一次匹配失敗之前結束或發生錯誤,則返回 EOF(檔案結束)。

示例 1:讀取整數和字串

以下程式碼從檔案中讀取整數和字串並列印它們。

以下是 C 庫函式 fscanf() 的示例。

#include <stdio.h>
#include <stdlib.h>

int main() {
   const char *filename = "example1.txt";
   FILE *file = fopen(filename, "r");

   // If the file does not exist, create and write initial data to it
   if (file == NULL) {
       file = fopen(filename, "w");
       if (file == NULL) {
           perror("Error creating file");
           return 1;
       }
       fprintf(file, "123 HelloWorld\n");
       fclose(file);
       file = fopen(filename, "r");
       if (file == NULL) {
           perror("Error opening file");
           return 1;
       }
   }

   int number;
   char word[50];

   if (fscanf(file, "%d %49s", &number, word) == 2) {
       printf("Number: %d, Word: %s\n", number, word);
   } else {
       printf("Failed to read data from file.\n");
   }

   fclose(file);
   return 0;
}

輸出

以上程式碼產生以下結果:

Number: 123, Word: HelloWorld

示例 2:讀取多個浮點數

以下程式碼從檔案中讀取三個浮點數並列印它們。

#include <stdio.h>
#include <stdlib.h>

int main() {
   const char *filename = "example2.txt";
   FILE *file = fopen(filename, "r");

   // If the file does not exist, create and write initial data to it
   if (file == NULL) {
       file = fopen(filename, "w");
       if (file == NULL) {
           perror("Error creating file");
           return 1;
       }
       fprintf(file, "3.14 2.718 1.618\n");
       fclose(file);
       file = fopen(filename, "r");
       if (file == NULL) {
           perror("Error opening file");
           return 1;
       }
   }

   float num1, num2, num3;

   if (fscanf(file, "%f %f %f", &num1, &num2, &num3) == 3) {
   if (fscanf(file, "%f %f %f", &num1, &num2, &num3) == 3) {
       printf("Numbers: %.2f, %.2f, %.2f\n", num1, num2, num3);
   } else {
       printf("Failed to read data from file.\n");
   }

   fclose(file);
   return 0;
}

輸出

執行以上程式碼後,我們將得到以下結果:

Numbers: 3.14, 2.72, 1.62

示例 3:從檔案讀取日期

以下程式碼讀取 DD/MM/YYYY 格式的日期並將其以 DD-MM-YYYY 格式列印。

#include <stdio.h>
#include <stdlib.h>

int main() {
   const char *filename = "example3.txt";
   FILE *file = fopen(filename, "r");

   // If the file does not exist, create and write initial data to it
   if (file == NULL) {
       file = fopen(filename, "w");
       if (file == NULL) {
           perror("Error creating file");
           return 1;
       }
       fprintf(file, "23/05/2024\n");
       fclose(file);
       file = fopen(filename, "r");
       if (file == NULL) {
           perror("Error opening file");
           return 1;
       }
   }

   int day, month, year;

   if (fscanf(file, "%d/%d/%d", &day, &month, &year) == 3) {
       printf("Date: %02d-%02d-%04d\n", day, month, year);
   } else {
       printf("Failed to read data from file.\n");
   }

   fclose(file);
   return 0;
}

輸出

以上程式碼的輸出如下:

Date: 23-05-2024
廣告