블로그 이미지
송시혁

calendar

1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Notice

Tag

Recent Post

Recent Comment

Recent Trackback

Archive

2013. 11. 13. 10:06 c++


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

int main()
{
  fstream fp;
  int score;

  fp.open("sample.txt", ios::in|ios::binary);
  //파일에서 읽기 모드, 이진파일 모드
  if(true==fp.fail())
  {
    return 1;
  }
  //실패시 종료
  while(fp.read((char *)&score, 4), false==fp.eof())
  {
    cout << setw(3<< right << score << endl;
  }
  //4바이트만큼 score에 4바이트 만큼 읽는다.
  //파일의 끝이 아닐 때 까지 while문은 반복한다.

  fp.seekg(4, ios::cur);
  //파일에 입력할때 지정한 만큼 파일 포인터가 이동한다.
  //1. 바이트 2. 이동 기준 설정. 여기서는 현재위치가 기준이다(ios:cur)
  //ios::end= 파일의 끝 위치에서 이동, ios::beg= 파일의 처음위치에서 이동.

  fp.read((char *)&score, 4);
  //다시 4바이트 만큼 읽는다.
  cout << setw(3<< right << score << endl;
  fp.close();

  return 0;
}


posted by 송시혁