简体   繁体   English

C ++中std :: ofstream的初始化向量

[英]Initialise vector of std::ofstream in c++

How do I initialise a vector of std::ofstream? 如何初始化std :: ofstream的向量? I am working on socket programming whereby I receive data for different symbols. 我正在进行套接字编程,从而接收不同符号的数据。 For each symbol, I need to assign a separate file handle to write data to it. 对于每个符号,我需要分配一个单独的文件句柄以向其中写入数据。 So, I need an array of std::ofstream. 因此,我需要一个std :: ofstream数组。 Now I want to initialise 100 of these std::ofstream. 现在,我要初始化其中的100个std :: ofstream。 But it does not work for me. 但这对我不起作用。 Please review the code below and suggest alternatives. 请查看下面的代码,并提出其他建议。

int main(){
std::vector <std::ofstream > myFilePointerMap;
std::ofstream  ofs;
    ofs.open("E:\\data\\test.txt", std::ios::app);
    for(int i=0;i<100;i++){
        myFilePointerMap.push_back( ofs);
    }
}

I am compiling using cygwin gcc in Windows eclipse. 我正在Windows Eclipse中使用cygwin gcc进行编译。

I get the following error while compiling: 编译时出现以下错误:

'std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private line 789, external location: D:\\cygwin64\\lib\\gcc\\x86_64-pc-cygwin\\4.8.3\\include\\c++\\bits\\ios_base.h C/C++ Problem 'std :: ios_base&std :: ios_base :: operator =(const std :: ios_base&)'是专用线路789,外部位置:D:\\ cygwin64 \\ lib \\ gcc \\ x86_64-pc-cygwin \\ 4.8.3 \\ include \\ c ++ \\ bits \\ ios_base.h C / C ++问题

Please help me with this. 请帮我解决一下这个。

EDIT: Let me explain what I need to done so that alternate solutions may also be supported. 编辑:让我解释一下我需要做些什么,以便还可以支持替代解决方案。 I need to create a object which listens to a socket. 我需要创建一个侦听套接字的对象。 Now this socket receives upto 100 different messages which need to be written to separate files, depending upon the id (1-100) received in individual messages. 现在,此套接字最多接收100条不同的消息,这些消息需要写到单独的文件中,具体取决于各个消息中收到的ID(1-100)。

At present, I have created a vector of strings containing filepaths. 目前,我已经创建了一个包含文件路径的字符串向量。 The declaration in header is as follows: 标头中的声明如下:

std::vector <std::string> filePathMap;

Then I initialise these filepaths in the constructor as follows: 然后,我在构造函数中初始化这些文件路径,如下所示:

for(int i=0;i<100;i++){
    filePathMap.push_back(" ");
}

Then for random indexes between 1 to 100, when I subscribe to a message, I set the relavant filepath as 然后对于1到100之间的随机索引,当我订阅一条消息时,我将相对文件路径设置为

filePathMap[j]=filePath;

There after whenever I receive a message, I open the file corresponding to filePathMap and index, write to it and close it. 之后,每当我收到一条消息,便打开与filePathMap和索引对应的文件,对其进行写入并关闭。

Originally, I wanted to create a vector of file pointers corresponding to these different symbols, so that when I receive a message, I can simply write to the file pointer, without the overhead of opening and closing each time. 最初,我想创建一个与这些不同符号相对应的文件指针向量,以便在收到消息时,可以简单地写入文件指针,而无需每次打开和关闭的开销。 Therefore, I defined in my header file vector of ofstreams as follows: 因此,我在ofstreams的头文件向量中定义如下:

    std::vector <std::ofstream > myFilePointerMap;

But in the constructor, when I put the following code, I get the error: 但是在构造函数中,当我放置以下代码时,出现了错误:

std::ofstream  ofs;
ofs.open("E:\\data\\test.txt", std::ios::app);
for(int i=0;i<100;i++){
    myFilePointerMap.push_back( ofs); //THIS DOES NOT COMPILE
}    

Note that I am only trying to initialise vector of myFilePointerMap to 100 so that when I assign myFilePointerMap later in the code, it should work fine. 请注意,我只是试图将myFilePointerMap的向量初始化为100,以便在以后的代码中分配myFilePointerMap时,它应能正常工作。

 myFilePointerMap[j].open(filePath.c_str(),std::ios::app);

Here, j can be anything from 0-99. 在此,j可以是0-99之间的任何值。

std::ofstream is not copyable, but are movable as of C++11. std::ofstream是不可复制的,但是从C ++ 11开始是可移动的。 Either std::move() the ofs into the vector or use a vector<> of (smart) pointers if your compiler does not support move. 无论是std::move()ofsvector或使用vector<>的(智能)指针,如果你的编译器不支持的举动。

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

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