繁体   English   中英

将三个文本文件组合成一个文本文件以构成一个句子C ++

[英]Combining three text files into one text file forming a sentence C++

我正在尝试使用Microsoft Visual Studio 2013在C ++中创建一个程序。该程序的主要目的是读取三个不同的文件(headlines1,headlines2和headlines3),并将它们放在一起形成一个文件,并在其中创建一个句子该输出文件。 我已经找到了可以使用的功能,但是该功能只能将3个文件读取并打印到控制台窗口中。 当我尝试将cout语句更改为输出文件时,我创建的输出文件为空白...我不知道该怎么做或如何构建代码。

    #include<fstream>
    #include<iostream>
    #include<cstdlib>

    using namespace std;

    void readingFile(string[], ifstream &); //Funtion Prototype
    int main()
    {
    string header1[50], header2[50], header3[50]; //Declaring array with 50 elements

    int size1, size2, size3;

    ifstream Fin, Fin2, Fin3;
    ofstream Fout;

    Fin.open("Headlines1.txt");   //Reading from these 3 files.
    Fin2.open("Headlines2.txt");
    Fin3.open("Headlines3.txt");
    if (!Fin || !Fin2 || !Fin3)    //Checking for unsuccessful open
    {
        cout << "Input file opening failed.\n";
        cin.ignore();
        return 1;
    }

    Fout.open("testingHeadlines.txt");  //Used for unsuccessful opening output
    if (!Fout)
    {
        cout << "Output file opening failed.\n";
        cin.ignore();
        return 2;
    }

    cout << "Building.... Editing....\n" << endl;
    cin.ignore();

    cout << "Headlines file 1 below:\n" << endl;
    readingFile(header1, Fin);  //Function call
    cout << endl << endl;
    cout << "Headlines file 2 below:\n" << endl;
    readingFile(header2, Fin2); //Function call
    cout << endl << endl;
    cout << "Headlines file 3 below:\n" << endl;
    readingFile(header3, Fin3); //Function call
    cout << endl << endl;

    cin.ignore();
    return 0;
    }

    //the function 'readingFile' 
    //Pre-conditions: Reads the contents from the files of Headlines1,2,and 3
    //Post-conditions: Prints out the contents.
    void readingFile(string[], ifstream &infile)
    {   
    char next;
    infile.get(next);
        while (!infile.eof())  //Reading until EndOfFile
        {
            cout << next;    //Problem is here?? I would think.
            infile.get(next);
        }
     } 

我不确定我在哪里说“问题在这里?” 问题出在哪里。 每次我将cout更改为outfile(我知道,我必须更改函数头中的参数)后,我都要打开outfile并且文件为空。

所有文件都包含随机单词/词组,放在一起将构成一个句子。 对于前。 标题1包含'*** Queen Jennifer * '**标题2包含'***进行脑部手术 ''**标题3包含'***食用30枚墨西哥辣椒后。* '**并且放在一起时应显示 ' Queen Jennifer吃了30颗墨西哥胡椒后进行了脑部手术。 ”,但文件中包含的单词/短语比我在示例中列出的要多。

当我在上面运行该程序时,我可以读取所有三个Headline文件,但是它们以向下的形式打印。 例如,我在控制台屏幕上的输出将是:

女王

詹妮弗

有脑外科

吃了30墨西哥胡椒之后

问题:让我的标题从左到右阅读。

将它们放入输出文件而不是控制台屏幕。

请帮助。

您可以替换这个...

void readingFile(string[], ifstream &infile)
{   
    char next;
    infile.get(next);
    while (!infile.eof())  //Reading until EndOfFile
    {
        cout << next;    //Problem is here?? I would think.
        infile.get(next);
    }
} 

...以及打给他们的电话,例如...

readingFile(header1, Fin);

...有了这个...

void readingFile(ifstream& infile, ofstream& fout)
{   
    char next;
    while (infile.get(next))  //Reading until EndOfFile or error
        if (next != '\n') // if not newline
           fout << next;    // stream to file
} 

并呼叫ala

readingFile(Fin, Fout);

这样, readingFile被告知在何处写入输出,并过滤出导致输出出现在不同行上的换行符。

这应该可以解决问题。

#include <fstream>
#include <string>

int main() {
    // Open the three files.
    std::ifstream file_1("Headlines1.txt");
    std::ifstream file_2("Headlines2.txt");
    std::ifstream file_3("Headlines3.txt");

    // Combine into one string.
    std::string content;
    content += std::string(std::istreambuf_iterator<char>(file_1),
        std::istreambuf_iterator<char>());
    content += std::string(std::istreambuf_iterator<char>(file_2),
        std::istreambuf_iterator<char>());
    content += std::string(std::istreambuf_iterator<char>(file_3),
        std::istreambuf_iterator<char>());

    // Output the string into a single file.
    std::ofstream output_file("testingHeadlines.txt");
    output_file << content;
}

我不确定您要对文件之间的间距做什么,但这对于您微调此代码来说应该并不难。

输出可能是这样的一个可能原因是infile.get(next)使用的std::ifstream::get() infile.get(next)是一种无格式的读取方法,这意味着它不会跳过空格和换行符\\n默认。 因此,您需要检查next值是否是这样的换行符:

if(next == '\\n') continue ;

在将其传递给cout << next 因此,您将在控制台屏幕上跳过打印换行符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM