以相反顺序c ++从文本文件中读取行 [英] Reading in lines from a text file in reverse order c++
问题描述
我需要找到一种从文件中读取最后6行数据的方法。
I need to find a way of reading in the last 6 lines of data from a file.
例如,如果我有
1
2
3
4
5
6
7
8
9
10
For example if I have 1 2 3 4 5 6 7 8 9 10
我需要阅读才能获得
10,9 ,8,7,6,5,4。
I need to read be able to get 10, 9, 8, 7, 6, 5, 4.
这些需要放入变量或字符串中以便稍后输出。目前我已经设法读取文件的最后一行,但我不知道如何读取其他5个数字。
These need to then be put into variables or strings to be outputted later. Currently I have managed to read in the last line of the file but I have no idea how to then read in the other 5 numbers.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
std::ifstream in("test.txt");
if (in.is_open())
{
std::vector<std::string> lines_in_reverse;
std::string line, line2;
while (std::getline(in, line))
{
// Store the lines in reverse order.
lines_in_reverse.insert(lines_in_reverse.begin(), line);
}
cout << line << endl;
while (std::getline(in, line2))
{
// Store the lines in reverse order.
lines_in_reverse.insert(lines_in_reverse.begin(), line2);
}
cout << line2 << endl;
}
cin.get();
return 0;
}
任何人都可以建议这样做吗?我不知道任何可以提供帮助的功能或方法。
Can anyone advise a way to this? I do not know of any functions or methods that can help.
编辑
此方法输出文件中的最后6个数字,但它们是向后的,我需要一种方法来反转它们并摆脱它打印出的空白。
This method outputs the last 6 numbers from the file however they are backwards and I need a way to reverse them and get rid of the whitespace it prints out.
我不确定如何使用反向以及需要哪些参数 - http://en.cppreference.com/w/cpp/algorithm/reverse
I'm unsure on how to use reverse and which arguments are required from this - http://en.cppreference.com/w/cpp/algorithm/reverse
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
char x;
ifstream f("test.txt", ios::ate);
streampos size = f.tellg();
for (int var = 1; var <= size; var++){
f.seekg(-var, ios::end);
f.get(x);
reverse(x);
cout << x;
}
cin.get();
return 0;
}
很多回复告诉我如何使用向量反转文本文件但不是最后6个数字是我需要的唯一信息。
Alot of the responses show me how to reverse the text file using vectors but not the last 6 numbers which is the only information I need.
问候
推荐答案
如何读取文件并反向打印,仅在三个代码陈述中(不包括声明和其他样板):
How to read a file and print it reverse, in only three statements of code (excluding declarations and other boilerplate):
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
void read_and_print_reverse_n(std::istream& is, const int n)
{
std::vector<std::string> v;
// This call reads all whitespace-delimited "words" from the input stream
// and appends them to the vector
std::copy(std::istream_iterator<std::string>(is),
std::istream_iterator<std::string>(),
std::back_inserter(v));
// Output the last `n` lines from the input
for (const auto i = v.rbegin();
i < v.rend() && i < v.rbegin() + n;
++i)
{
std::cout << *i << '\n';
}
}
int main()
{
read_and_print_reverse_n(std::cin, 6);
}
参考文献
std::copy
std::istream_iterator
std::back_inserter
这篇关于以相反顺序c ++从文本文件中读取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!