Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clear a file while run time

Status
Not open for further replies.

shasa

Programmer
Aug 14, 2010
17
IR
i am going to use a file that its data is deleted after some processes ,and another data will be typed while the program is runnnig ,during run time this proccess will occure many times.

how i can clear old data before writing the new one?


regards
 
This should work:

To read the already-existing data, open the file with the status="old" specifier in the "open" statement. Once you've read the data you need, close the file with the "close" statement. Between the "open" and "close" above, you should have no "write" statements to write the file yet. This part is probably familiar to you.

Now, to write over the file (after you've closed it in the above step), open it again, but this time, with the status="replace" specifier, write to it as you need, and then close it.

Obviously, to do it multiple times, you'd put it in a do loop or something like that, which depends on your program structure.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
tnx for your reply

actually i have write and read for the old one 2 so can i use "old"?
 
Yes, you can. I only said "you should have no "write" statements to write the file yet" because I thought that it was a read-only step. I think the main point of my post is that you want status="replace" when you want everything to be erased from the file. That specifier creates a new file if one doesn't exist, and if it does, it deletes it and creates a new one with the same name, which is the same as erasing everything in it.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
If you want to rewrite a file, you have just to rewind it !

Example :

Code:
PROGRAM test
INTEGER :: i
OPEN(15,'test.dat')
DO i=1,100
  REWIND(15)
  WRITE(15,*) i
ENDDO
END

At the end of that program, the file 'test.dat' will contain a single line with the value 100

About your actual program, I don't see where you allocate the array MATRIX. I expected the following instruction just after reading the value of N :
Code:
 ALLOCATE (matrix(n,n))

In addition, I don't find your code structure very clear :
it has many DO WHILE loops which should be replace by a simple loops. For instance, the loop on A1 should be written :
Code:
  DO A1=-1,1,2

Idem for all the loops managed by integers. I am pretty sure that you have many mistakes induced by the DO WHILE. For instance, look at the following sequence :

Code:
         A=0
         B=0
         DO WHILE (B<=N)
           DO WHILE (A<=N)
             
              READ (30,*)MATRIX(A,B)
              A=A+1
           END DO
           B=B+1
         END DO

It should be written :

Code:
         DO B=1,N
           DO A=1,N
              READ (30,*)MATRIX(A,B)
           END DO
         END DO

And the result is not at all the same because you have initialized A at the wrong place. A=0 should be just before the loop on A !

At last, I would replace the overall sequence by a single instruction :
Code:
   READ(30,*) matrix

There are other difficulties with your programming style, mainly because you use tabulations. Don't use them please. It is pretty much impossible to reproduce the indentation you get with your editor and your code appears horrible with my text editor !
 
If you want to rewrite a file, you have just to rewind it !

But what if your old file has more lines than the new one? Wouldn't that mean that you have leftovers from the old version of the file at the end if you rewind it?

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
>> But what if your old file has more lines than the new one? >> Wouldn't that mean that you have leftovers from the old
>> version of the file at the end if you rewind it?

No : the first WRITE statement after the REWIND deletes automatically the old version !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top