😸
[C] fscanf 関数
機能
ファイルからデータを読み取る関数[1]
使用例
123 Tom
と表示する
\n
は改行を表す エスケープシーケンス[2]
コード
FILE *fp = fopen("data.txt", "r");
int id;
char name[20];
fscanf(fp, "%d %s", &id, name);
printf("%d %s\n", id, name);
fclose(fp);
全文
#include <stdio.h>
void main() {
FILE *fp = fopen("data.txt", "r");
int id;
char name[20];
fscanf(fp, "%d %s", &id, name);
printf("%d %s\n", id, name);
fclose(fp);
}
実行結果
123 Tom
456 Alice
を変数から表示する
コード
FILE *fp = fopen("data.txt", "r");
int num;
char str[20];
fscanf(fp, "%d %s", &num, str);
printf("%d %s\n", num, str);
fclose(fp);
全文
#include <stdio.h>
void main() {
FILE *fp = fopen("data.txt", "r");
int num;
char str[20];
fscanf(fp, "%d %s", &num, str);
printf("%d %s\n", num, str);
fclose(fp);
}
実行結果
456 Alice
Discussion