获取输入文本文件的内容,并以相反的单词顺序将其写入输出文件。您的程序可以忽略换行符。您将需要使用数组。
(不必要的垃圾被删除)Aaaaah,恐慌,请帮忙!
编辑:到目前为止我所拥有的。现在仍然对如何扭转词序感到失望。//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;
}
我成功地逐字逐句读取输入文件。我可以弄清楚除了如何反转单词顺序然后写入output.txt之外的所有内容 这是我们的第一个扭转事情顺序的计划,是的。
好的,我能猜到的最好的是:
//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;
}
输入部分工作正常。输出只是重复每次迭代的输入的最后一个字。
开玩笑,除了输出文件的实际写入之外的所有内容都有效。通过在初始化中删除MAXSIZE
之后的“-1”来校正终端中的输出。调整以类似方式写入文件的代码并不能解决重复的“工作”问题。 (输入文件的最后一个字)写入output.txt
现在是相关代码:
//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] << " ";
}
如果我在i>=0
中将i=0
更改为for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i)
(我错误地输入了第一次尝试),那么终端中的cout
就是完美的。无法弄清楚如何让输出文件不被重复“工作”。它为什么这样做?注意:输出到终端是可选的,所以我真的不在乎为什么在完成分配方面希望i
在之前的for循环中被赋值为0
答案 0 :(得分:0)
另一种算法:
std::vector<char>
或std::string
中。std::reverse
分别反转每个单词。std::reverse
反转整个文件。答案 1 :(得分:-1)
这就是我最终做到的方式:
//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;
}
答案 2 :(得分:-1)
所以,让我尽力帮助你。 也许,它会有所帮助。 获取输入文本文件的内容,并以相反的单词顺序将其写入输出文件。您的程序可以忽略换行符。您将需要使用数组。
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; }