将三个文本文件合并成一个文本文件,形成一个句子C++; 我试图用微软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); } } #包括 #包括 #包括 使用名称空间std; 无效读取文件(字符串[],ifstream&)//功能原型 int main() { 字符串header1[50],header2[50],header3[50];//声明包含50个元素的数组 int size1、size2、size3; ifstream-Fin,Fin2,Fin3; 流式流量计; Fin.open(“Headlines1.txt”);//读取这3个文件。 Fin2.open(“Headlines2.txt”); Fin3.open(“Headlines3.txt”); 如果(!Fin | | |!Fin2 | |!Fin3)//检查打开失败 { cout

将三个文本文件合并成一个文本文件,形成一个句子C++; 我试图用微软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); } } #包括 #包括 #包括 使用名称空间std; 无效读取文件(字符串[],ifstream&)//功能原型 int main() { 字符串header1[50],header2[50],header3[50];//声明包含50个元素的数组 int size1、size2、size3; ifstream-Fin,Fin2,Fin3; 流式流量计; Fin.open(“Headlines1.txt”);//读取这3个文件。 Fin2.open(“Headlines2.txt”); Fin3.open(“Headlines3.txt”); 如果(!Fin | | |!Fin2 | |!Fin3)//检查打开失败 { cout,c++,arrays,visual-studio,function,computer-science,C++,Arrays,Visual Studio,Function,Computer Science,这应该能奏效 #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 int

这应该能奏效

#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;
}
#包括
#包括
int main(){
//打开这三个文件。
std::ifstream文件_1(“Headlines1.txt”);
std::ifstream文件_2(“Headlines2.txt”);
std::ifstream文件_3(“Headlines3.txt”);
//合并成一条线。
std::字符串内容;
content+=std::string(std::istreambuf_迭代器(文件1),
std::istreambuf_迭代器();
content+=std::string(std::istreambuf_迭代器(文件2),
std::istreambuf_迭代器();
content+=std::string(std::istreambuf_迭代器(文件3),
std::istreambuf_迭代器();
//将字符串输出到单个文件中。
std::of流输出_文件(“testingHeadlines.txt”);

输出文件您的输出之所以是这样的一个可能原因是
std::ifstream::get()
infle.get(下一个)中使用
是一种非格式化读取方法,这意味着默认情况下它不会跳过空格和换行符
\n
。因此您需要检查
下一个
值是否是如下所示的换行符:

if(next=='\n')继续;


在将其传递给
cout之前,您可以替换此

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);
    }
} 
…用这个

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
} 

通过这种方式,
readingFile
被告知将输出写入何处,并过滤掉导致输出出现在不同行上的换行符。

系统(“cat文件1文件2文件3”)
;除了打开它之外,你什么时候在这段代码中使用过你的
Fout
文件?是的!这很有效!非常感谢!但我现在遇到了另一个问题。文件中的所有内容现在都在一行上,就像我想要的一样,但都在一行上。我怎样才能做到“皇后珍妮弗”@CesarEsco看起来你的句子被切掉了…我猜你想用一个额外的空格来替换换行符…只需添加一个
else fout
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
} 
readingFile(Fin, Fout);