943,693 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 22021
  • C++ RSS
Sep 9th, 2003
0

Read/write to same file > once, Help

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fishman44 is offline Offline
1 posts
since Sep 2003
Jun 19th, 2006
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xgills is offline Offline
3 posts
since Apr 2006
Jun 19th, 2006
0

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

Where do you dig these 2 year old threads from? I am closing this thread.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: C++ CGI Scripts
Next Thread in C++ Forum Timeline: Coding Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC