반응형

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


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






반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C언어' 카테고리의 다른 글

명령어 라인으로 텍스트 파일 합치기  (0) 2017.02.18
단어 바꾸기  (0) 2017.02.18
텍스트 파일과 이진 파일  (0) 2017.02.17
도서 관리 프로그램  (0) 2017.02.17
줄 번호 붙이기  (0) 2017.02.17

+ Recent posts