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

允我心安 提交于 2020-01-03 06:35:50

问题


Take the contents of an input text file and write them in reverse order of words to an output file. Your program can ignore line breaks. You will need to use arrays.

(unnecessary crap removed) Aaaaah, panic, please help!

EDIT: What I have so far. Still lost on how to reverse word order now.

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

What I've got successfully reads the input file word for word. I can figure out everything except how to reverse the word order for then writing to output.txt This is our first program reversing the order of things, yes.

EDIT 2:

Okay, so the best I can guess is this:

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

The input section works fine. Output just repeats last word of input for each iteration.

EDIT 3:

Just kidding, everything except actual writing of output file works. Output in the terminal is corrects by getting rid of the "-1" after MAXSIZE in the initialization. Adjusting the code that writes the file in a similar way does not solve the repeating "works." (final word of input file) written to output.txt

EDIT 4:

Relevant code is now:

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

If I change i>=0 to i=0 (which I mistakenly typed first try) in for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i), then cout in terminal is perfect. Can't figure out how to get output file to not be repeated "works." Why is it doing that? Note: output to terminal optional, so I don't really care why that wants i assigned to 0 in previous for loop in terms of being able to finish the assignment


回答1:


Another algorithm:

  1. Map the file into memory or read it into std::vector<char> or std::string.
  2. Reverse each word individually with std::reverse.
  3. Reverse the entire file with std::reverse.
  4. Write the file back if it was read into a container.



回答2:


This is how I ended up doing it:

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



回答3:


So, let me try to help you the best way I can. Maybe, it will be helpful. Take the contents of an input text file and write them in reverse order of words to an output file. Your program can ignore line breaks. You will need to use arrays.

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


来源:https://stackoverflow.com/questions/25007955/how-to-take-input-file-reverse-word-order-write-to-output-file-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!