如何在c++中反转和显示文本文件中的值
How to reverse and display the values in a text file in C++?
我的目标是能够以反向顺序读取/显示文件中的数字。
我编写了以正常顺序执行的代码(并且它确实有效),我只需要让程序以相反的顺序显示
文件只是一个文本文件,里面有数字。
我有:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file("numbers.dat");
string content;
while(file >> content) {
cout << content << ' ';
}
return 0;
}
将文件中的数字存储在容器中,从后往前打印。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream file("numbers.dat");
std::string content;
std::vector<std::string> numbers;
while(file >> content)
numbers.push_back(content);
for(int i = numbers.size() - 1; i >= 0; i--)
std::cout << numbers[i] << ' ';
std::cout << std::endl;
return 0;
}
相关文章:
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 动态填充的组合框不显示文本,列表未定义
- 使用 fltk 时,文本未显示在已编译的窗口中
- PyQT - 在 QTableWidget 单元格中显示垂直文本
- 如何使用IExecuteCommand和动词在上下文菜单外壳扩展中显示本地化文本和自定义图标?
- 如何在 qt c++ 中更新 QWindow,文本不显示
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- sf::文本不显示?
- 在QT C++编辑Qtable模型数据时,我需要得到一个小盒子,我需要显示编辑文本,这怎么可能?
- QTreeWidgetItem 突出显示文本问题
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- 如何通过单击按钮在QTextEdit中显示文本?
- 在C Windows程序中显示文本文件内容
- 如何在重新调整窗口大小时使文本显示在窗口中心,visualc++
- 打开 GL 文本显示时未处理的异常
- 如何要求输入后的文本显示下一行在c++中
- 如何将文件的路径(或任何文本)显示/附加到编辑框中?(C++ Win32 API)
- 将格式化文本显示到 QTextEdit 就像在 C 的 printf 中一样