C

[C] 242 파일에서 특정 문자열 검색 strstr

qkrwngus 2021. 2. 8. 19:58

Desc :

 

 


Source Code :

#include <stdio.h>
#include <string.h>

void main()
{
	FILE *fp;
	char buff[200];
	int line = 1;

	fp = fopen("C:\\Users\\w4135\\test.txt", "r");

	if (fp == NULL)
	{
		perror("파일 읽기 개방 에러");
		return;
	}

	while (!feof(fp)) // 파일의 끝에 올 때까지
	{
		fgets(buff, 200, fp);	// 200바이트를 읽어와 buff에 저장

		if (strstr(buff, "hello"))	// 버퍼에서 문자열 검색 --> 검색 성공시 위치 반환
		{
			printf("Line(%2d): %s", line, buff);
		}
		line++;
	}

	fcloseall();
}

 


Result :