#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; }
|