C++/C++ ifstream, ofstream

ifstream, ofstream C++98, C++11

재윤 2023. 12. 2. 19:26
반응형
#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++ > C++ ifstream, ofstream' 카테고리의 다른 글

ifstream, ofstream  (0) 2023.12.01