读书人

怎么改动下面的代码让其可以把文件的内

发布时间: 2012-02-28 13:06:36 作者: rapoo

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

读书人网 >C++

热点推荐