셀맨1 2017. 2. 18. 00:32
반응형

파일에서 특정한 단어를 찾아서 파일 이름과 단어가 위치한 줄 번호를 출력하는 프로그램을 작성하라.


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
30
31
32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int main(void)
{
    FILE *fp=NULL;
    char name[SIZE];
    char word[SIZE];
    char line[SIZE];
    int num=0;
    printf("파일 이름: ");
    gets(name);
    if((fp=fopen(name,"r"))==NULL)
    {
        printf("파일오픈오류\n");
        exit(1);
    }
    printf("탐색할 단어: ");
    gets(word);
    while(!feof(fp))
    {
        num++;
        fgets(line,SIZE,fp);
            if(strstr(line,word)!=NULL)
            {
                printf("%s: %d    %s",name,num,line);
            }
    }
    fclose(fp);
    return 0;
}
cs






반응형