您打算解决此问题的一种重要方法是以下算法:
- 将文件加载到缓冲区中,并以空字符终止。
- 定位一个指向
p
最后一个缓冲槽位置的指针。
- 虽然
p
不指向缓冲区的开头,但请执行以下操作:
- 如果字符是换行符 (
'\n'
) 则
- 将字符串通过换行符 (
p+1
) 发送到标准输出。
p
用空字符覆盖指向的换行符。
- 减少
p
一个字符位置。
- 上述循环完成后,还剩下一行:第一行。将它发送到标准输出,你就完成了。
或者我被引导去相信。需要考虑的重要事项如下:
- 该算法是否适用于空文件?
- 该算法是否适用于仅包含换行符的文件?
- 该算法是否适用于没有尾随换行符的多行文件?
- 该算法是否适用于没有尾随换行符的单行文件?
- 该算法是否适用于带有尾随换行符的多行文件?
- 该算法是否适用于带有尾随换行符的单行文件?
话虽如此,这里有一个潜在的候选人:
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
// assume the file to reverse-print is the first
// command-line parameter. if we don't have one
// we need to leave now.
if (argc < 2)
return EXIT_FAILURE;
// will hold our file data
std::vector<char> data;
// open file, turning off white-space skipping
ifstream inf(argv[1]);
inf.seekg(0, inf.end);
size_t len = inf.tellg();
inf.seekg(0, inf.beg);
// resize buffer to hold (len+1) chars
data.resize(len+1);
inf.read(&data[0], len);
data[len] = 0; // terminator
// walk the buffer backwards. at each newline, send
// everything *past* it to stdout, then overwrite the
// newline char with a nullchar (0), and continue on.
char *start = &data[0];
char *p = start + (data.size()-1);
for (;p != start; --p)
{
if (*p == '\n')
{
if (*(p+1))
cout << (p+1) << endl;
*p = 0;
}
}
// last line (the first line)
cout << p << endl;
return EXIT_SUCCESS;
}
输入
I like the red color
blue is also nice
and green is lovely
but I don't like orange
输出
but I don't like orange
and green is lovely
blue is also nice
I like the red color
一种相当简单的方法
有更简单的方法可以做到这一点,我将在评论中解释每一步。有可能你不能使用这样的东西,但重要的是你要了解什么时候可以使用:
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
// assume the file to reverse-print is the first
// command-line parameter. if we don't have one
// we need to leave now.
if (argc < 2)
return EXIT_FAILURE;
// collection that will hold our lines of text
vector<string> lines;
// read lines one at a time until none are returned
// pushing each line in to our vector.
ifstream inf(argv[1]);
string line;
while (getline(inf, line))
lines.push_back(line);
inf.close();
// a LOT happens in the next single line of code, and
// I will try to describe each step along the way.
//
// we use std::copy() to copy all "items" from
// a beginning and ending iterator pair. the
// target of the copy is another iterator.
//
// our target iterator for our formatted ouput
// is a special iterator class designed to
// perform an output-stream insertion operation
// (thats the << operator) to the stream it is
// constructed with (in our case cout) using each
// item we give it from our copy-iteration. to use
// this class the "copied" item must support the
// traditional insertion operator <<, which of
// course, std::string does. after each item is
// written, the provided suffix (in our case \n)
// is written as well. without this all the lines
// would be ganged together.
//
// lastly, to glue this together (and the whole
// reason we're here), we use a pair of special
// iterators designed to work just like the regular
// begin() and end() iterators you're familiar with,
// when traversing forward in a sequence, but these
// ones, rbegin() and rend(), move from the last
// item in the sequence to the first item, which is
// *exactly* what we need.
copy(lines.rbegin(), lines.rend(),
ostream_iterator<string>(cout, "\n"));
// and thats it.
return EXIT_SUCCESS;
}
输入
I like the red color
blue is also nice
and green is lovely
but I don't like orange
输出
but I don't like orange
and green is lovely
blue is also nice
I like the red color
更新:合并用户输入
为第二个版本合并用户输入的示例是:
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
// collection that will hold our lines of text
vector<string> lines;
do
{ // prompt the user
cout << "Sentance (<enter> to exit): ";
string line;
if (!getline(cin, line) || line.empty())
break;
lines.push_back(line);
} while (true);
// send back to output using reverse iterators
// to switch line order.
copy(lines.rbegin(), lines.rend(),
ostream_iterator<string>(cout, "\n"));
return EXIT_SUCCESS;
}