Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;将向量的内容写入txt类?_C++_Vector_Fstream_Ofstream - Fatal编程技术网

C++ C++;将向量的内容写入txt类?

C++ C++;将向量的内容写入txt类?,c++,vector,fstream,ofstream,C++,Vector,Fstream,Ofstream,所以我做了一个函数,从两个文件中读取行并将它们写入一个向量。调用函数两次,每个文件调用一次。现在,我应该如何将向量的内容写入一个新文件?还有,我做得对吗 #include "stdafx.h" #include <iostream> #include <string> #include <vector> #include <fstream> #include <algorithm> using namespace std; bool g

所以我做了一个函数,从两个文件中读取行并将它们写入一个向量。调用函数两次,每个文件调用一次。现在,我应该如何将向量的内容写入一个新文件?还有,我做得对吗

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
bool getFile(string filename, vector<string> & vecOfStrs){
    ifstream in(filename.c_str());
    if (in.fail()) {
        cout << "No such file" << "\n";
        return 1;
    }
    string str;
    while (getline(in, str))
    {
        if (str.size() > 0)
            vecOfStrs.push_back(str);
    }
    in.close();
    return true;
}

int main(){
    vector<string> A;
    string lecCourse;
    cout << "First file: ";
    cin >> lecCourse;
    string lab_ex;
    cout << "Second file: ";
    cin >> lab_ex;
    string exit;
    cout << "Name of exit file: " << "\n";
    cin >> exit;
    bool result = getFile(lecCourse, A);
    bool result2 = getFile(lab_ex, A);
    ofstream output;
    output.open(exit.c_str());
    if (output.fail()) {
        cout << "error" << "\n";
        return 1;
    }
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
bool getFile(字符串文件名、向量和向量){
ifstream-in(filename.c_str());
if(in.fail()){
当然可以;
弦乐实验室;
试验室;
字符串退出;
不能退出;
bool result=getFile(lecourse,A);
bool result2=getFile(lab_ex,A);
流量输出;
output.open(exit.c_str());
if(output.fail()){
不能包含
#包括
#包括
#包括
#包括
使用名称空间std;
载体A;
bool getFile(字符串文件名、向量和向量){
ifstream-in(filename.c_str());
if(in.fail()){
当然可以;
弦乐实验室;
试验室;
字符串退出;
不能退出;
bool result=getFile(lecourse,A);
bool result2=getFile(lab_ex,A);
流量输出;
output.open(exit.c_str());

对于In i=0;请将您的代码格式化并发布所有相关代码。您所发布的代码将不会编译,因为它有几个语法错误。建议——如果您不熟悉C++的某些方面,则应该编写较小的程序。不需要所有这些代码开始计算如何将向量的内容写入文件。n(){std::vector v={1,2,3};std::ofstream output(“out.txt”);…}
是您所需要的全部,然后尝试填写
。嗯,Radoslav..请确保这个问题回答正确。
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
vector<string> A;
bool getFile(string filename, vector<string> & vecOfStrs){
ifstream in(filename.c_str());
if (in.fail()) {
    cout << "No such file" << "\n";
    return 1;
}
string str;
while (getline(in, str))
{
    if (str.size() > 0)
        vecOfStrs.push_back(str);
}
in.close();
return true;
}
int main()
    {
string lecCourse;
cout << "First file: ";
cin >> lecCourse;
string lab_ex;
cout << "Second file: ";
cin >> lab_ex;
string exit;
cout << "Name of exit file: " << "\n";
cin >> exit;
bool result = getFile(lecCourse, A);
bool result2 = getFile(lab_ex, A);
ofstream output;
output.open(exit.c_str());
for(int i=0;i<A.size();i++)
{
    output<<A[i]<<"\n";
}
if (output.fail()) {
    cout << "error" << "\n";
    return 1;
    }
}