Problem:

Input File:

[sample.txt

0.00MB](https://blog.kakaocdn.net/dna/WEEud/btqA6oyqdRE/AAAAAAAAAAAAAAAAAAAAAF6o8u7LK5SB9AIdNSz_-IRJgLPtbZjLxGCM5EIoOvqI/sample.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=AakSu1DOyqICwWZkF6%2F%2FvvgPYME%3D&attach=1&knm=tfile.txt)

Execution Result:

소스 Code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <stdio.h> #include <string.h> #include <stdlib.h> void main() { FILE *fp; char buf[80]; int n = 0; if ((fp = fopen("sample.s", "r")) == NULL) { fprintf(stderr, "file not found...\n"); exit(1); } while(fgets(buf, sizeof(buf), fp) != NULL) { n += get_token_num(buf); } fclose(fp); printf("Number of token = %d\n", n); } int get_token_num(char *bp) { char *cp; int n = 0; for(cp = strtok(bp, " \t\n"); cp != NULL; ) { n++; cp = strtok(NULL, " \t\n"); } return(n); }

Explanation:

exit()를 사용하기 위해 stdlib.h 헤더파일을 include 시켜주었습니다.