함수 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("\nEntered string : %s\n", s);
return(0);
} 출력
Enter a string : Peter! Entered string : Peter!
함수 fscanf()
fscanf() 함수는 C 언어로 주어진 스트림에서 형식화된 입력을 읽는 데 사용됩니다. 실패하면 0을 반환합니다. 그렇지 않으면 성공하면 입력 문자열을 반환합니다.
다음은 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 = \n%s\n", str1 );
fclose(fp);
return(0);
} 출력
First word = This