Working with array of files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Working with array of files

 
0
  #1
Jan 29th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Working with array of files

 
0
  #2
Jan 29th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Re: Working with array of files

 
0
  #3
Jan 29th, 2005
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:

  1. #include <fstream.h>
  2. #include <iostream.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <iomanip>
  6. #include "sort.h"
  7.  
  8. char InputFileName[16];
  9. ifstream InputFile;
  10. ofstream TempOutputFile;
  11. int ClumpSize = 0;
  12. int i, j;
  13. double ClumpArray[1000];
  14. double Number;
  15. int Counter = 0;
  16. fstream F[10];
  17.  
  18. int main()
  19. {
  20. //cout << "Please enter the name of the file to be sorted: \n";
  21. //cout << "(include the path if not in the current directory):\n";
  22. //cout << '\n';
  23. //cin >> InputFileName;
  24.  
  25. InputFile.open("program1.dat", ios::in);
  26. TempOutputFile.open("Output", ios::out);
  27.  
  28. for (i = 0; i < 5; i++)
  29. {
  30. F[Counter%5].open("Output2", ios::out);
  31. Counter++;
  32. }
  33.  
  34. if (F[Counter%5].fail())
  35. {
  36. cout << "Error opening the temporary files." << '\n';
  37. cout << "Exiting program...";
  38. exit (-1);
  39. }
  40.  
  41. if (InputFile.fail())
  42. {
  43. cout << "Error opening the input file " << InputFileName << ".\n";
  44. cout << "Exiting program...";
  45. exit (-1);
  46. }
  47.  
  48. if (TempOutputFile.fail())
  49. {
  50. cout << "Error opening the output file. \n";
  51. cout << "Exiting program...";
  52. exit (-1);
  53. }
  54.  
  55.  
  56. for (i = 0; i < 10; i++)
  57. //only tried for 10 numbers. However, it will work for 100, 1,000, or 100,000. I stopped there.
  58. {
  59. InputFile >> Number;
  60. ClumpArray[i] = Number;
  61. }
  62.  
  63. QuickSort(ClumpArray, 0, 9);
  64.  
  65. Counter = 0;
  66. for (i = 0; i < 10; i++) //this part does not work correctly
  67. { //Printing to my temp file does work
  68. TempOutputFile << setprecision(15);
  69. TempOutputFile << ClumpArray[i] << '\n';
  70. F[Counter%5] << setprecision(15); //?????????????
  71. F[Counter%5] << ClumpArray[i]; //?????????????
  72. Counter++;
  73. }
  74.  
  75. InputFile.close();
  76. TempOutputFile.close();
  77. F[Counter%5].close(); //?????????????????
  78.  
  79. return 0;
  80. }

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC