65,211
社区成员
发帖
与我相关
我的任务
分享
ifstream test;
test.open(fileDir);
//get the length of the file;
int temp = test.tellg();//record the current site.
test.seekg(0,ios_base::end);//move to the end of the file
int len = test.tellg();//get the length
test.seekg(temp);//move back to the original site
char* buffer = new char[len];
test.read(buffer,len);
#include "iostream"
#include "fstream"
using namespace std;
int main()
{
char fileDir[] = "test.txt";
ifstream test;
test.open(fileDir, ios_base::binary); // 这里,用2进制打开
//get the length of the file;
int temp = test.tellg();//record the current site.
test.seekg(0,ios_base::end);//move to the end of the file
int len = test.tellg();//get the length
test.seekg(temp);//move back to the original site
char* buffer = new char[len + 1];
test.read(buffer,len);
buffer[len] = 0;
cout<<buffer<<endl;
return 0;
}
char* buffer = new char[len];
test.read(buffer,len);
//是不是应该换成
char* buffer = new char[len-temp];
test.read(buffer,len-temp);