首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

用 ifstream 读入当中有结束符的文件

2012-09-28 
用 ifstream 读入中间有结束符的文件要点:用二进制打开文件获取文件大小分配内存读入代码如下:C/C++ code/

用 ifstream 读入中间有结束符的文件
要点:
用二进制打开文件
获取文件大小
分配内存
读入


代码如下:

C/C++ code
// read a file into memory#include <iostream>#include <fstream>using namespace std;int main () {  int length;  char * buffer;  ifstream is;  is.open ("test.txt", ios::binary );  // get length of file:  is.seekg (0, ios::end);  length = is.tellg();  is.seekg (0, ios::beg);  // allocate memory:  buffer = new char [length];  // read data as a block:  is.read (buffer,length);  is.close();  cout.write (buffer,length);  delete[] buffer;  return 0;}


[解决办法]
什么意思?你写的,帮顶

热点排行