#아래와 같이 한줄에 단어 하나씩 기록된 텍스트 파 일 “sample.s”이 있다. 이들 단어를 모두 연결했다 고 가정했을 때, 각 단어의 시작 위치를 단어와 함 께 출력하고, 마지막에는 총 길이를 출력한다(16진 수 사용).

<출력결과>

이 프로그램의 가장 중요한 부분은 단어의 시작주소(start)를 출력하기위해 포인터를 사용해야 한다는 점이다.
<소스코드>
#include <stdio.h> /* project #1 : 텍스트 파일내 단어의 총 수를 카운트*/
#include <string.h>
#include <stdlib.h>
int get_token_num(char *bp,int num,int *start);
int main() {
FILE *fp;
char buf[80];
int num=0;
int start=0;
if ((fp = fopen("sample.s", "r")) == NULL) {
fprintf(stderr, "file not found...\n"); exit(1);
}
while(fgets(buf, sizeof(buf), fp) != NULL) {
num+= get_token_num(buf,num,&start);
}
fclose(fp);
printf("Numb of token = %.2X\n",num);//단어 수 출력
}
int get_token_num(char *bp,int num,int *start)
{
char *cp;
int i;
for(cp = strtok(bp, " \t"); cp != NULL; ) {
printf("%.2X: ",*start); //주소 출력
*start = *start + strlen(cp) -1 ;//전 단어의 길이 뒤에 숫자 = 주소
for(i=0;i<strlen(cp);i++){
num = strlen(cp)-1; //단어의 수
printf("%c",cp[i]); //단어 출력
}
cp = strtok(NULL," \t");
}
return (num);
}
'ASSEMBLY' 카테고리의 다른 글
System Software _ project 5 (0) | 2019.06.26 |
---|---|
MAKING ASSEMBLYSYSTEM SOFTWARE (0) | 2019.06.26 |
System Software _ project 2 (0) | 2019.06.26 |
System Software _ project 1 (0) | 2019.06.26 |