| | |
Working with array of files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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:
Thanks for the help and any suggestions you may have.
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:
C++ Syntax (Toggle Plain Text)
#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.
Last edited by alc6379; Jan 31st, 2005 at 9:14 pm. Reason: added [code] tags
![]() |
Similar Threads
- working with an array (PHP)
- Problem declaring class object array (C++)
- Working with header files ??? (C)
- array of files in C (C)
Other Threads in the C++ Forum
- Previous Thread: Using Division Operator
- Next Thread: template issues - need expert debugger!
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






