입출력 클래스 <fstream>

박호준·2021년 8월 26일
0

ifstream : 입력클래스

#include <iostream>
#include <string>
#include <fstream>
int main(void)
{
  std::	ifstream readFile.open("test.txt"); // 파일 열기
  
  if (readFile.is_open())
  {
    while (!readFile.eof())
    {
      std::string str;
      std::getline(readFile,str); // string 형태로 받아와서 쓸 수 있음  <string> 헤더
      cout << str << endl;
    }
  	readFile.close(); // 파일 닫기
   }
   return (0);
}

ofstream : 출력클래스

#include <iostream>
#include <fstream>
#include <string>
 
int main()
{
	ofstream writeFile;
    writeFile.open("test.txt"); // 파일 열기
    
    // 1. char[] 문자열
    char	arr[6] = "hopark";
    writeFile.write(arr, 5);
    
    // 2. string 문자열
    std::string str = "hjpark";
    writeFile << str;
    // writeFile.write(str.c_str(), str.size());
    // str.c_str() : string >>> const string 으로 변환
    
    writeFile.close();
    return (0);
 }
profile
hopark

0개의 댓글