如何获取输入文件,反转词序,以C++写入输出文件

How to take input file, reverse word order, write to output file in C++

本文关键字:文件 C++ 输出 获取 输入 何获取      更新时间:2023-10-16

获取输入文本文件的内容,并按单词的相反顺序将它们写入输出文件。 程序可以忽略换行符。 您将需要使用数组。

(删除不必要的废话(啊,恐慌,请帮忙!

编辑:到目前为止我有什么。 现在仍然不知道如何颠倒单词顺序。

//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 以便能够完成分配

另一种算法:

  1. 将文件映射到内存或将其读入std::vector<char>std::string
  2. std::reverse单独反转每个单词。
  3. std::reverse 反转整个文件。
  4. 如果文件已读入容器,请将其写回。

这就是我最终这样做的方式:

//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; }