Read/write to same file > once, Help

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Sep 2003
Posts: 1
Reputation: fishman44 is an unknown quantity at this point 
Solved Threads: 0
fishman44 fishman44 is offline Offline
Newbie Poster

Read/write to same file > once, Help

 
0
  #1
Sep 9th, 2003
Please help a newby trying to code first program.

I want to be able to write data to a file, then read from the same file.

I can manage this OK, but If I set-up a loop to do the same procedure again I get an error message telling me it cannot open the file for a second time.

Below is a simple form of my problem. I want to write some random numbers to a file, read from it and display the results, then go back and repeat the procedure a user defined number of times. I MUST use the same file though, as I will want to do this loop a great many times.

My code is:

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. main ()
  7. {
  8.  
  9. fstream file1;
  10. fstream file2;
  11.  
  12. int i, a, b, c, d;
  13.  
  14. srand(time(NULL));
  15.  
  16.  
  17. cout << "how many times ";
  18. cin >> d;
  19.  
  20. for( c = 1; c <= d;c++ )
  21. {
  22.  
  23. // *****************************
  24.  
  25. cout << endl;
  26. cout << " try " << c << " ";
  27.  
  28.  
  29. // writing to file
  30. file1.open( "test.dat", ios::out);
  31.  
  32. if (!file1)
  33. {
  34. cerr << "Unable to open test.dat 1.";
  35. exit(1);
  36. }
  37.  
  38. for( i = 1; i <= 5;i++ )
  39. {
  40. a = (rand() % 100) + 1; // random nunber from 1 to 100
  41. file1 << a << endl; // write to file
  42. }
  43. file1.close();
  44.  
  45. // reading from file
  46. file2.open( "test.dat", ios::in);
  47. if (!file2)
  48. {
  49. cerr << "Unable to open test.dat 2.";
  50. exit(1);
  51. }
  52. while( !file2.eof() )
  53. {
  54. file2 >> b;
  55. cout << b << " ";
  56. }
  57. file2.close();
  58. }
  59. }

The output I get (on a PC) is:

How many times 3

try 1 82 62 24 24 40 40
try 2 Unable to open test.dat 2


I have tried everything I know (not much) and really need some help.

John
Last edited by WolfPack; Jun 19th, 2006 at 9:55 pm.
Quick reply to this message  
Join Date: Apr 2006
Posts: 3
Reputation: xgills is an unknown quantity at this point 
Solved Threads: 0
xgills xgills is offline Offline
Newbie Poster

Re: Read/write to same file > once, Help

 
0
  #2
Jun 19th, 2006
why do you close the file? there is a different cursor (wrong technical name I am sure but easier to think about it as a cursor) for inputs and outputs...since u are using fstream instead of ofstream or ifstream, you can read and write with same object. Open the file before the loop, then close it after, move the cursors back to beginning at start of loop with file1.seekp(0L, ios::beg) file1.seekg(0L, ios::beg). This will make teh code neater but I am not sure if that's an actual problem. I can't check it on my own computer since I accidentally delted my C++ software the other day...but only opening it once will make it easier to debug, even if it doesnt solve the problem.
Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Read/write to same file > once, Help

 
0
  #3
Jun 19th, 2006
Where do you dig these 2 year old threads from? I am closing this thread.
バルサミコ酢やっぱいらへんで
Quick reply to this message  
Closed Thread

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