| | |
Read/write to same file > once, Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2003
Posts: 1
Reputation:
Solved Threads: 0
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:
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
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)
#include <iostream.h> #include <fstream.h> #include <stdlib.h> #include <stdio.h> main () { fstream file1; fstream file2; int i, a, b, c, d; srand(time(NULL)); cout << "how many times "; cin >> d; for( c = 1; c <= d;c++ ) { // ***************************** cout << endl; cout << " try " << c << " "; // writing to file file1.open( "test.dat", ios::out); if (!file1) { cerr << "Unable to open test.dat 1."; exit(1); } for( i = 1; i <= 5;i++ ) { a = (rand() % 100) + 1; // random nunber from 1 to 100 file1 << a << endl; // write to file } file1.close(); // reading from file file2.open( "test.dat", ios::in); if (!file2) { cerr << "Unable to open test.dat 2."; exit(1); } while( !file2.eof() ) { file2 >> b; cout << b << " "; } file2.close(); } }
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.
•
•
Join Date: Apr 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
![]() |
Similar Threads
- Read and write in the same file (C)
- Concurrent read/write to a file (C++)
- Read from multiple file and Write into 1 file using loop (C++)
- How to read/write to an MS-Excel file?? (C++)
- read and write csv file using csv module (Python)
- Question about file read/write (Java)
Other Threads in the C++ Forum
- Previous Thread: C++ CGI Scripts
- Next Thread: Coding Question
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node 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 word wordfrequency wxwidgets





