如何获取输入文件,反转词序,以C++写入输出文件
How to take input file, reverse word order, write to output file in C++
获取输入文本文件的内容,并按单词的相反顺序将它们写入输出文件。 程序可以忽略换行符。 您将需要使用数组。
(删除不必要的废话(啊,恐慌,请帮忙!
编辑:到目前为止我有什么。 现在仍然不知道如何颠倒单词顺序。
//Kristen Korz
//CIS 22A
//This program reads an input file and writes the words in reverse order to an output file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//create and link input...
ifstream inputFile;
inputFile.open("input.txt");
//...and output files
ofstream outputFile;
outputFile.open("output.txt");
//error message for file open fail
if (inputFile.fail())
cout << "Error opening the file.n";
//constant for max size
const int MAXSIZE = 1024;
//string array and temporary-use string
string words[MAXSIZE];
string str;
//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
words[i] = str;
}
inputFile.close();
//for showing if read correctly
cout << endl;
for (int i = 0; i < MAXSIZE; ++i)
cout << words[i] << endl;
system("pause");
return 0;
}
我所拥有的是逐字读取输入文件。 我可以弄清楚所有内容,除了如何反转单词顺序然后写入输出.txt这是我们第一个颠倒事物顺序的程序,是的。
编辑2:
好的,所以我能猜到的最好的是:
//constant for max size
const int MAXSIZE = 1024;
//string array and temporary-use string
string words[MAXSIZE];
string str; //note: variables will be used for output loops too
//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
words[i] = str;
}
inputFile.close();
//for showing if read correctly
cout << endl;
for (int i = 0; i < MAXSIZE; ++i)
cout << words[i] << endl;
//for writing in reverse word order to output file
for (int i = MAXSIZE-1; (outputFile << str) && (i >= 0); --i)
{
words[i] = str;
}
outputFile.close();
//for showing if written correctly
for (int i= MAXSIZE-1; i >= 0; --i)
{
cout << words[i] << endl;
}
输入部分工作正常。 输出只是重复每次迭代的最后一个输入单词。
编辑3:
开玩笑,除了实际写入输出文件之外,其他所有内容都可以正常工作。 终端中的输出通过在初始化中MAXSIZE
后删除"-1"来更正。 调整以类似方式写入文件的代码并不能解决写入输出的重复"works."(输入文件的最后一个字.txt
编辑4:
相关代码现在为:
//constant for max size
const int MAXSIZE = 1024;
//string array and temporary-use string
string words[MAXSIZE];
string str; //note: variables will be used for output loops too
//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
words[i] = str;
}
inputFile.close();
//for showing if read correctly
cout << endl;
for (int i = 0; i < MAXSIZE; ++i)
cout << words[i] << " ";
//for writing in reverse word order to output file
for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i)
{
words[i] = str;
}
outputFile.close();
//for showing if written correctly
for (int i = MAXSIZE; i >= 0; --i)
{
cout << words[i] << " ";
}
如果我在for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i)
中将i>=0
更改为i=0
(我第一次尝试错误地输入了(,那么终端中的cout
是完美的。 无法弄清楚如何使输出文件不重复"工作"。 它为什么要这样做? 注意:输出到终端可选,所以我真的不在乎为什么在之前的 for 循环中i
分配给 0 以便能够完成分配
另一种算法:
- 将文件映射到内存或将其读入
std::vector<char>
或std::string
。 - 用
std::reverse
单独反转每个单词。 - 用
std::reverse
反转整个文件。 - 如果文件已读入容器,请将其写回。
这就是我最终这样做的方式:
//Kristen Korz
//CIS 22A
//This program reads an input file and writes the words in reverse order to an output file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//create and link input...
ifstream inputFile;
inputFile.open("input.txt");
//...and output files
ofstream outputFile;
outputFile.open("output.txt");
//error message for file open fail
if (inputFile.fail())
cout << "Error opening the file.n";
//constant for max size
const int MAXSIZE = 1024;
//string array and temporary-use string
string words[MAXSIZE];
string str; //note: variables will be used for output loops too
//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
words[i] = str;
//for showing in terminal if read correctly
cout << words[i] << " " << i << " ";
}
inputFile.close();
cout << endl;
//something wrong with for loop resulting in i apparently not updating
for (int i = MAXSIZE - 1; (outputFile << str) && (i >= 0); --i)
{
if (str.size() < MAXSIZE)
{
//for showing in terminal if written correctly
cout << words[i] << " " << i << " ";
}
}
outputFile.close();
cout << endl;
system("pause");
return 0;
}
所以,让我尽力帮助你。也许,这会有所帮助。获取输入文本文件的内容,并按单词的相反顺序将它们写入输出文件。程序可以忽略换行符。您将需要使用数组。
inputFile.open (" input.txt ");
outputFile.open (" output.txt ");
if (inputFile.fail()) {
cout << "input.txt not found" << endl;
return -1;
}
if (outputFile.fail()) {
cout << "not able to open output.txt" << endl;
return -1;
}
int i = 0;
int count = 0;
string words[1000];
while (inputFile >> words[i]) {
count++;
i++;
}
for (int j = count; j >= 0; j--) {
outputFile << words[j] << " ";
}
inputFile.close();
outputFile.close();
return 0; }
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到