Reading multiple files concurrently

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elikhom
    New Member
    • Jul 2007
    • 8

    Reading multiple files concurrently

    Is there any way to open multiple files concurrently and for example read the first line of each and the do some task with them, then read the second line of all of them and do some task again with them, etc until all of them are finished?

    Code:
    for (int i = 0; i < (int)products.size(); i++)
      {
        string fileName = folder + "\\" + products[i] + "_" + parameters_.getDate() + ".csv";
        fileNames_.push_back(fileName);
        ifstream fileStream(fileName.c_str());
        fileStreams_.push_back(&fileStream);
        eof_.push_back(false);
      }
    
      for (int i = 0; i < (int)fileStreams_.size(); i++)
      {
        if (fileStreams_[i]->is_open())
    	  {
    		  string dummy;
    		  getline (*fileStreams_[i],dummy);
                      cout << i << ": " << dummy << endl;
             }
      }
    It seems that when I reach the second loop all the fileStreams are closed :(
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    To do this, you'll need to look into a concept called threading. Its details vary from OS to OS and it can be pretty tricky. Googling "your OS + threading + C++" will probably get you some useful information.

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      It seems that when I reach the second loop all the fileStreams are closed :(
      And can you guarantee you can open one individually? Have you run a modified version of the code that works on single filestreams?

      This is not a multithreading issue. You're not doing concurrent actions. You just simply have multiple streams open at once. That's not a problem. See, cout, cin, and cerr being open at once.

      Comment

      Working...