C 語言中的 scanf() 和 fscanf()
scanf() 函式
scanf() 函式用於以 C 語言格式從 stdin 中讀取輸入。它會返回寫入它的整個字元數;如果未寫任何字元,則返回負值。
以下是 C 語言中 scanf() 的語法:
int scanf(const char *characters_set)
以下是 C 語言中 scanf() 的示例:
示例
#include <stdio.h>
int main () {
char s[20];
printf("Enter a string : ");
scanf("%s", s);
printf("
Entered string : %s
", s);
return(0);
}輸出
Enter a string : Peter! Entered string : Peter!
fscanf() 函式
fscanf() 函式用於從 C 語言中給定流中讀取格式化輸入。如果失敗,它將返回零。否則,如果成功,它將返回輸入字串。
以下是 C 語言中 fscanf() 的語法:
int fscanf(FILE *stream_name, const char *set_of_characters)
以下是 C 語言中 fscanf() 的示例:
示例
#include <stdio.h>
#include <stdlib.h>
int main () {
char str1[10];
int year;
FILE * fp;
fp = fopen ("file.txt", "w+");
fputs("This is demo text!", fp);
rewind(fp);
fscanf(fp, "%s", str1);
printf("First word =
%s
", str1 );
fclose(fp);
return(0);
}輸出
First word = This
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP