如何获取输入文件,反向字序,写入C ++中的输出文件 [英] How to take input file, reverse word order, write to output file in C++

查看:608
本文介绍了如何获取输入文件,反向字序,写入C ++中的输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

(删除不必要的垃圾)Aaaaah,panic,请帮忙!

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

编辑:仍然失去了如何反转字顺序现在。
// Kristen Korz
// CIS 22A
//此程序读取输入文件,并以相反的顺序将字写入输出文件。

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

我已经成功读取输入文字。我可以弄清楚除了如何颠倒字序,然后写到output.txt之外的一切。
这是我们第一个程序颠倒顺序的事情,是的。

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.

编辑2:好的,所以最好的我可以猜到这是:

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.

编辑3:只是开玩笑,除了实际写输出文件的工作。终端中的输出通过在初始化中除去MAXSIZE之后的-1来校正。调整以类似方式写入文件的代码不会解决重复的工作。 (输入文件的最后一个字)写入output.txt

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

编辑4:相关代码现在是:
//最大大小的常量
const int MAXSIZE = 1024;
//字符串数组和临时使用字符串
string words [MAXSIZE];
string str //注意:变量也将用于输出循环

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



如果我将i> = 0改为i = 0我错误地键入第一次尝试)在for(int i = MAXSIZE;(outputFile < = 0); --i),那么cout in terminal是完美的。无法弄清楚如何获取输出文件,不要重复工作。为什么会这样做?注意:输出到终端是可选的,所以我真的不在乎为什么我希望我在前面的for循环中分配为0能够完成赋值

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

推荐答案

所以,让我尽力帮助你最好的方式。
也许,这将是有益的。
获取输入文本文件的内容,并将它们以字的相反顺序写入输出文件。您的程序可以忽略换行符。您将需要使用数组。

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

这篇关于如何获取输入文件,反向字序,写入C ++中的输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆