[C++ 문법] ifstream, ofstream C++98, C++11

반응형
#include <iostream>
#include <fstream>

int main(int argc, char **argv)
{
	std::string s1 = argv[2];
	std::string s2 = argv[3];
	std::string inFileName = argv[1];
	std::string outFileName = inFileName + ".replace";
	int len1 = s1.length();
	int len2 = s2.length();
	if (inFileName.length() == 0 || len1 == 0 || len2 == 0)
		return (std::cout << "Arguments length is 0\\n",	0);
	std::ifstream infile;
	infile.open(inFileName.c_str());
	std::ofstream outfile;
	outfile.open(outFileName.c_str());
	if (infile.fail() || outfile.fail())
		return (std::cout << "Open Failed\\n", 0);
	infile.close();
	outfile.close();
}
  • 위 코드에서 open을 할 때 inFileName과 outFileName이 있다. 여기서 const char *로 바꿔주지 않으면 std::string을 사용하는 것이기 때문에 C11이 되어버린다. const char *로 캐스팅 해주어야 C98로 된다.
반응형

'개발 지식 > C++' 카테고리의 다른 글

STL, 컨테이너, 반복자 개념  (0) 2024.02.09
roundf  (0) 2024.01.24
[C++ 문법] C++ 함수 포인터 배열  (0) 2023.12.02
[C++ 문법] ifstream, ofstream  (0) 2023.12.01
erase, insert  (0) 2023.12.01