[42Seoul] CPP Module 01 - ex04

수빈·2022년 1월 11일
0

42CPP

목록 보기
9/11
post-thumbnail

fstream 헤더

fstream 헤더는 c++에서의 파일 입출력 헤더이다.std::coutstd::cin과 유사하게 시프트 연산을 통해 사용할 수 있다.

fstream 헤더에는 총 세 가지 클래스가 존재한다.

  1. ifstream
  2. ofstream
  3. fstream
  1. 파일 불러오기
std::ifstream fin(filename);
	//파일 연결
	if (fin.fail())
	{
		std::cerr << "Error: No '" << filename << "' found" << std::endl;
		return (1);
	}
  1. 파일 내용 출력해 보기
char out;
	while (fin.get(out))
		std::cout << out;
  1. s1문자열이 있는지 확인하기
while (buf.find(s1) != std::string::npos)
  1. s1 내용을 s2 내용으로 바꾸기
{
	index = buf.find(s1);
	buf.erase(index, s1.length());
	//s1지우고
	buf.insert(index, s2);
	//s2넣기
}
  1. 해당 파일을 .replace 확장자 붙여서 출력하기
std::ofstream fout(filename + ".replace");
fout << buf;
profile
42Seoul -soooh ~ 2022.04

0개의 댓글