Does anyone know how to use an array of files? The array is defined as:
fstream F[10].
I have 10 temp files that I need to use but am not sure how to open these temp files or write to them. I have a counter to keep up with which file to write to:
F[Counter%5].
What I need to do is read 1000 records from a file, sort them, and write the 1st 1000 records to F[0], the next 1000 records to F[1]...etc. How would you open the array of files or write to the array of files? I have never worked with multiple files like this. Thanks for any help! :p

>How would you open the array of files or write to the array of files?
Show us your code. What you're doing sounds very tricky and error prone. I'd like to make sure that you're doing it right.

Ok. Here's a description of the original problem. I have to do a balanced 5-way sort merge. I have to ask the user for the filename, which I have hard-coded just to save me from having to type it each time. The input file is 1,000,000 double-precision floating point numbers. The maximum size of my array is 1000. I have to leave the input file like it is, no changing of it. I am to read 1000 records into the array, sort the array using QuickSort, and write them to File #1. Then, read the next 1,000 records from the input file, sort them and write them to File #2. Keep going like this and alternately writing between the 5 files until the end-of-file for the input file. I have the program where it will read in the 1000 records, sort them but cannot get them to write to the appropriate file. The instructor stated that "suppose you have an array of files, fstream F[10]. Set up a counter intitiated to 0. Using
F[Counter%5], then the first 1,000 records will be written to F[0]. Increment the counter each time. It will write to F[1], F[2], F[3], and F[4] each time."
I have never worked with an array of files and am not sure exactly how this works. My code is only for dividing the "clumps" (1,000 records) and writing them to a file. I have not gotten to the second phase, or the merging process.

Here's my code:

#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include "sort.h"

char InputFileName[16];
ifstream InputFile;
ofstream TempOutputFile;
int ClumpSize = 0;
int i, j;
double ClumpArray[1000];
double Number;
int Counter = 0;
fstream F[10];

int main()
{
  //cout << "Please enter the name of the file to be sorted: \n"; 
  //cout << "(include the path if not in the current directory):\n";
  //cout << '\n';
  //cin >> InputFileName;
  
  InputFile.open("program1.dat", ios::in);
  TempOutputFile.open("Output", ios::out);
  
  for (i = 0; i < 5; i++)
  {
    F[Counter%5].open("Output2", ios::out);
    Counter++;
  }
  
  if (F[Counter%5].fail())
  {
    cout << "Error opening the temporary files." << '\n';
    cout << "Exiting program...";
    exit (-1);
  }
  
  if (InputFile.fail())
  {
    cout << "Error opening the input file " << InputFileName << ".\n";
    cout << "Exiting program...";
    exit (-1);
  }
    
  if (TempOutputFile.fail())
    {
      cout << "Error opening the output file. \n";
      cout << "Exiting program...";
      exit (-1);
    }
    
    
  for (i = 0; i < 10; i++)    
//only tried for 10 numbers. However, it will work for 100, 1,000, or 100,000. I stopped there.                    
  {
    InputFile >> Number;
    ClumpArray[i] = Number;
  }
    
  QuickSort(ClumpArray, 0, 9);
  
  Counter = 0;                          
  for (i = 0; i < 10; i++)              //this part does not work correctly
  {                                         //Printing to my temp file does work 
    TempOutputFile << setprecision(15);
    TempOutputFile << ClumpArray[i] << '\n';
    F[Counter%5] << setprecision(15);          //?????????????
    F[Counter%5] << ClumpArray[i];             //?????????????
    Counter++;
  }
  
  InputFile.close();
  TempOutputFile.close();
  F[Counter%5].close();                    //?????????????????
  
  return 0;
}

Thanks for the help and any suggestions you may have. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.