#include "stdafx.h"
#include "string.h"
#include "iostream.h"
#include <stdio.h>
#include <fstream.h>
int main(int argc, char* argv[])
{
fstream f("e:\\test.txt",ios::in | ios::out | ios::trunc | ios::binary);
int i;
cout<<"Enter an integer:"<<endl;
cin>>i;
f.write((char*)(&i),sizeof(i));
int j=0;
f.seekg(0,ios::beg);
f.read((char*)(&j),sizeof(j));
cout<<j;
getchar();
return 0;
}
得到文件长度
ifstream in("readme.txt");
...
streampos pos = in.tellg(); // save current position
in.seekg(0, ios::end);
cout << "file length =" << in.tellg() << endl;
in.seekg(pos); // restore saved position