如何在C ++中取输入文件,反向字顺序,写入输出文件

问题描述 投票:-1回答:3

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

(不必要的垃圾删除)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之外的所有内容这是我们的第一个程序,它颠倒了事物的顺序,是的。

编辑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”来校正终端中的输出。调整以类似方式写入文件的代码并不能解决重复的“工作”问题。 (输入文件的最后一个字)写入output.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] << " ";
    }

如果我在i>=0中将i=0更改为for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i)(我错误地输入了第一次尝试),那么终端中的cout就是完美的。无法弄清楚如何让输出文件不被重复“工作”。它为什么这样做?注意:输出到终端可选,所以我真的不在乎为什么要在之前的for循环中将i指定为0才能完成赋值

c++ reverse fstream
3个回答
0
投票

另一种算法:

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

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

-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; }
© www.soinside.com 2019 - 2024. All rights reserved.