读取用c ++写入多个文件

时间:2016-05-15 19:27:57

标签: c++ file-io merge

我想从用户那里获取多个文件地址,然后在struct.txt文件和struct.txt文件的其他程序导出文件中读取和写入二进制模式。请指导我。 Importing - merge exporting - unmerge

#include <iostream>
#include <string>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
int tedad_file=0; // get count of files from user //

int main()
{
    int tr=0;
    cout << "please enter count of files you want to merge ";
    cin >>  tedad_file;
    cout << "\n";
    std::string *files[tedad_file];
    int counter=0;
    int temp=0;
    for(;temp<tedad_file;temp++) // getting file's address from user and add them into arrays of string (files variable)
    {
        cout << "Lotfan address file " << temp + 1 << " vared konid: \n";
        cin >> *files[temp];
    }

    std::ofstream output_file( "D:\\struct.txt", std::ios::binary ) ;
    int x=0;
    for(;x<tedad_file;x++) // for - read content of files to merge them into struct.txt ---- for example tedad_file is 3
    {
        std::ifstream first_file((char *)&files[tedad_file], std::ios::binary ) ;
        output_file << first_file.rdbuf();
    }
    return 0;
}

3 个答案:

答案 0 :(得分:1)

您应该使用std::vector<std::string> filesfiles.push_back(filename)例如:

int main()
{
    std::vector<std::string> files;

    cout << "please enter count of files you want to merge ";
    int file_count = 0;
    cin >> file_count;

    for (int temp = 0; temp<file_count; temp++)
    {
        cout << "Lotfan address file " << " vared konid: \n";
        std::string filename;
        cin >> filename;
        files.push_back(filename);
    }

    std::ofstream output_file("D:\\struct.txt", std::ios::binary);
    for (auto file : files)
    {
        std::ifstream fin(file, std::ios::binary);
        output_file << fin.rdbuf();
    }

    return 0;
}

否则使用new运算符。但new方法容易出错。例如:

int main()
{
    cout << "please enter count of files you want to merge ";
    int tedad_file = 0; 
    cin >> tedad_file;
    cout << "\n";

    std::string *files = new std::string[tedad_file];
    for (int temp = 0; temp<tedad_file; temp++) 
    {
        cout << "Lotfan address file " << " vared konid: \n";
        cin >> files[temp];
    }

    std::ofstream output_file("D:\\struct.txt", std::ios::binary);
    for (int temp = 0; temp<tedad_file; temp++)
    {
        std::ifstream first_file(files[temp], std::ios::binary);
        output_file << first_file.rdbuf();
    }

    delete[]files;
    return 0;
}

答案 1 :(得分:1)

你为什么写这一行:std::string *files[tedad_file];?您必须首先为此指针分配内存。如果你在这里没有使用指针并做了类似的事情会更好:std::string files[tedad_file]。您的代码没有为数组中的字符串分配内存,这就是为什么存在段错误的原因。

答案 2 :(得分:0)

通过这种方式解决

int tedad_file=0; // Daryaft tedad file jahate edgham //

int main()
{
int tr=0;
cout << "Lotfan Tedad file jahate edgham vared konid: ";
cin >>  tedad_file;
cout << "\n";
std::string files[tedad_file];
int counter=0;
int temp=0;
for(;temp<tedad_file;temp++)
{
cout << "Lotfan address file " << temp + 1 << " vared konid: \n";
cin >> files[temp];
}
streampos size1;
std::ofstream output_file( "D:\\test.txt", std::ios::binary ) ;
int x=0;
string dd;
for(;x<tedad_file;x++)
{
dd = files[x];
std::ifstream first_file(dd.c_str(), std::ios::binary ) ;
output_file << first_file.rdbuf();
size1 = first_file.tellg();
cout << size1;
}

return 0;
}