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

怎么改动下面的代码让其可以把文件的内容倒叙输出

2012-02-28 
如何改动下面的代码让其可以把文件的内容倒叙输出#includeiostream#includefstreamusingnamespacestd

如何改动下面的代码让其可以把文件的内容倒叙输出
#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);

    return   0;
}
Input:   test.txt.txt

0123456789
 

Output
   
9876543210

 



[解决办法]
只是倒序输出而已,只要把buffer从后往前输出即可
[解决办法]
#include <algorithm>

reverse(buffer,buffer+length);

热点排行