使用堆栈反转txt文档
Reversing a txt document using stacks
好吧,我正在写一个相对简单的程序,它从txt文档中提取字符,将它们从堆栈中弹出并保存到第二个文件中,但顺序相反,例如:
你好!
ereht olleh
我有一些代码,我一直在调整它,但它执行不正确。
#include <iostream>
#include <fstream>
#include <string>
include <stack>
using namespace std;
void reverse(ifstream & infile, ofstream & outfile); //function prototype
void main ()
{
ifstream infile; // declaring a file for input
ofstream outfile; // declaring a file for output
infile.open ("myInputFile.txt");
if (!infile)
{ cout << "File could not be opened." << endl;
}
outfile.open ("myOutfile.txt");
reverse (infile, outfile);
infile.close();// Close both files
outfile.close();
system("pause");
}
void reverse (ifstream & infile, ofstream & outfile)
{
string s;
stack<string> mystack;
getline(infile, s); // try to read a line of text from the file
while (getline(infile, s)) // while not end of file
{
mystack.push(s);
getline(infile, s); // try to read another line
}
while (!mystack.empty())
{
s = mystack.top();
outfile << s << endl; // write s to the outfile
mystack.pop();
}
}
编辑:对不起,我忘了最重要的部分。。问题。当程序执行时,它不会对输出文件执行任何操作。这让我相信这是我的逻辑中关于相反部分的部分。
编辑:编辑:好吧,我一直在胡搞,我仍然无法让它做什么,它现在打印到输出文件,但它只是把第2行的内容放在第1行。
示例:输入文件:
Hello world
How are things
输出文件:How are things
我想要的输出要么是所有字符颠倒,要么只是单词顺序颠倒。
示例"dlrow olleh"或"世界你好">
很抱歉,这是我的第一篇帖子,但我正在努力寻求帮助。
提前谢谢!
感谢您的帮助@ooga。流行音乐让我走上了正轨,尽管这不是我的问题。
我意识到我使用的是字符串堆栈,而我本应该使用Char堆栈来做我想要的事情并获得我想要的输出。
程序现在运行良好。
-Titan
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何在文档文件夹中创建目录
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 关于类的 Python 文档 - 对C++的引用不正确
- C++/autoconf 等效于文档库要求.txt的要求是什么?
- mbed:使用 USB 文档库编译会导致错误
- C++ VISUAL STUDIO:GLFWwindow没有初始化,即使它是在文档中编写的并且以前工作过
- 使用 yaml-cpp 更新 YAML 文档的节点和值
- 在加载 MSHTML 文档之前从 MSHTML 文档中删除无效的 URL
- 如何在C++中表示JSON文档的递归对象结构?
- 如何查找 Redshift Maya API 文档?
- 如何使用值模板编写文档测试用例?
- 如何使用 Gradle DSL(领域特定语言)上的文档?
- ncurses 找不到任何关于cchar_t的文档
- 提取狮身人面像文档中的C++代码片段
- 关于 API 文档和设备操作 在助手-sdk-cpp 上
- 创建 txt 文档C++
- 在TXT文档的一部分中查找信息并将其存储在变量中
- 使用堆栈反转txt文档