64,282
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream in("char.txt");
in.seekg(0, ios::end); //设置文件指针到文件流的尾部
streampos ps = in.tellg(); //读取文件指针的位置
cout << "File size: " << ps << endl;
in.seekg(0);
char *ch;
ch=new char[ps];
for(int i=0;i<ps;i++)
{
in.get(ch[i]);
}
cout << ch<<endl;
delete[] ch;
in.close();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream in("char.txt");
in.seekg(0, ios::end); //设置文件指针到文件流的尾部
streampos ps = in.tellg(); //读取文件指针的位置
cout << "File size: " << ps << endl;
in.seekg(0);
char *ch;
ch=new char[ps-1]; //这里改了
for(int i=0;i<ps;i++)
{
in.get(ch[i]);
}
cout << ch<<endl;
delete[] ch;
in.close();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream in("char.txt");
in.seekg(0, ios::end); //设置文件指针到文件流的尾部
streampos ps = in.tellg(); //读取文件指针的位置
cout << "File size: " << ps << endl;
in.seekg(0);
char *ch;
ch=new char[(int)ps+1];
for(int i=0;i<ps;i++)
{
in.get(ch[i]);
}
ch[ps]=0;
cout << ch<<endl;
delete[] ch;
in.close();
return 0;
}
ch=new char[(int)ps+1]; //改 ch=new char[ps];
for(int i=0;i<ps;i++)
{
in.get(ch[i]);
}
ch[ps] = 0; //加 字符串结束符
cout << ch<<endl;