c++

11월 11일 파일 클래스

송시혁 2013. 11. 11. 18:38

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ofstream output;    //output 객체 생성
  output.open("text.txt");  
  //output 객체로 파일 open, text.txt파일이 생성이 된다.
  output << "즐거운 프로그래밍!!!" <<endl;
  text.txt 파일 안에 내용이 써진다.
  
  output.close();//닫는다. c에서 close()함수와 같다.

  return 0;
}

output 객체를 사용시 ofstream을 사용한다. 
파일과 관련되 입출력은 fstream헤더 파일에서 제공한다.
객체.open("파일 이름"); , 점은 객체 구성원에 대한 참조 연산자.

//------------------------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream input;    //입력 파일 객체 생성.
  char message[80];
  
  input.open("test.txt"); //파일을 연다. 
  input >> message;  //파일에 내용을 message 버퍼에 저장
  cout << message << endl;//화면에 출력.(파일에 내용을)
  input.close();  


  return 0;
}