 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
Abhishek
Joined: 22 Dec 2010 Posts: 28
|
Posted: Wed Jan 12, 2011 2:19 pm Post subject: Unformatted files and BACKSPACE |
|
|
Hi
I am writing complex nos. to a file using unformatted sequential WRITE. I am writing these in batches of 310 complex nos. in a loop and then 93 complex nos. in the end.
After all of them have been written, I now need to read them back in reverse order, and I was trying to use BACKSPACE. However, on using BACKSPACE and reading the nos. back in, I get the following error:
110: Unformatted record is corrupt
I understand the complex no. storage is 8 bytes. When 310 complex nos. are written in a single record, 5 bytes are added to the start and end (i dont know exactly why 5 bytes are needed), making the record length to 310x8+10=2490. The end record is 93x8+10=754 long.
These nos. are as per the length of the file created.
I am using the following to open the file:
OPEN(3,FILE='FOO.FIL',FORM='UNFORMATTED',STATUS='REPLACE')
I have tried to do this operation without the end record and adding RECL=2490 to the OPEN Statement. Result is the same error.
Can somebody please throw some light ?
Thanks
Abhishek |
|
Back to top |
|
 |
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2402 Location: Yateley, Hants, UK
|
Posted: Wed Jan 12, 2011 2:54 pm Post subject: |
|
|
These are only guesses:
If you closed the file after writing, and then re-opened it, the pointer is at the beginning, and you can't BACKSPACE.
If you kept the file open, you can BACKSPACE once, then read the last record. However, you will need to BACKSPACE twice to get at the record before that, and (assuming that you want to read the records in reverse order) you will need to BACKSPACE twice before reading each record. The reason is that each time you READ a record, the pointer to where you are in the file moves to the next record, so you have to move it back two records to get at the start of the record before the one you just read.
Was this of any use?
Eddie |
|
Back to top |
|
 |
Abhishek
Joined: 22 Dec 2010 Posts: 28
|
Posted: Wed Jan 12, 2011 3:26 pm Post subject: |
|
|
I am not closing the file.
I am using this in following fashion.
For each ith read,
BACKSPACE(3)
READ(3)
BACKSPACE(3)
This way pointer should keep on getting backward. |
|
Back to top |
|
 |
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2402 Location: Yateley, Hants, UK
|
Posted: Wed Jan 12, 2011 3:56 pm Post subject: |
|
|
My second comment applies. It has to be
BACKSPACE
READ ! for the last record
BACKSPACE ! points you at the beginning of the last record
BACKSPACE ! points you at the beginning of the previous record
READ !reads the correct record
BACKSPACE
BACKSPACE
etc.
With only one BACKSPACE after the first time, you try to read a long record when your last record is short.
To check this, write the last record long as well (310 not 93 items). Put some data in that you will recognise. You will see that you keep reading the last record.
Eddie |
|
Back to top |
|
 |
Abhishek
Joined: 22 Dec 2010 Posts: 28
|
Posted: Wed Jan 12, 2011 4:34 pm Post subject: |
|
|
These lines are in a subroutine that gets called every time a READ has to be made.
Therefore, for every read, effectively the BACKSPACE statement is being called two times.
the error is thrown even for the first READ. |
|
Back to top |
|
 |
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2402 Location: Yateley, Hants, UK
|
Posted: Thu Jan 13, 2011 12:42 am Post subject: |
|
|
I can't duplicate the error:
Code: | PROGRAM COMP
COMPLEX A(310), B(93), C(310), D(93)
OPEN(3,FILE='FOO.FIL',FORM='UNFORMATTED',STATUS='REPLACE')
A = 1.0
B = 2.0
C = 3.0
D = 4.0
WRITE(3) A
WRITE(3) B
BACKSPACE(3)
READ(3) D
BACKSPACE(3)
BACKSPACE(3)
READ(3) C
WRITE(*,*) C(1)
WRITE(*,*) D(1)
STOP
END |
This works, giving (1.000,0.000) and (2.000,0.000) whereas if D and C hadn't read the numbers printed would have been different! What does your code look like?
E |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Thu Jan 13, 2011 3:40 am Post subject: |
|
|
I'd suggest you consider a fixed length record, direct access file.
Open the file, with RECL= the maximum expected length (in bytes) = 2480 from what you have indicated.
OPEN(UNIT=13,FILE='FOO.FIL',FORM='UNFORMATTED',ACCESS='DIRECT',RECL=2480,STATUS='REPLACE',IOSTAT=iostat)
If (iostat /= 0) then
...
UNIT=3 is a risky number to use, I always use a number > 10
Then write the records with a REC= for the record number, starting from 1.
The last record can be written (give the REC= as the next record number) As the I/O list is smaller, it will be padded out.
Then to read in reverse order, simply use the appropriate record number in REC=
END= is not a valid error test in a direct access file, so change to ERR=
John |
|
Back to top |
|
 |
Abhishek
Joined: 22 Dec 2010 Posts: 28
|
Posted: Fri Jan 14, 2011 10:38 am Post subject: |
|
|
Actually I was using arrays to read and write in batches, something like
WRITE(3) ((BUF(N8,N9),N8=1,31),N9=1,10)
and reading similarly
I found DIRECT access file to be much more reliable. Only thing is that RECL is variable in my case and can theoretically go upto 100x10x8=8000. |
|
Back to top |
|
 |
IanLambley
Joined: 17 Dec 2006 Posts: 506 Location: Sunderland
|
Posted: Fri Jan 14, 2011 5:18 pm Post subject: |
|
|
Try this
Code: |
PROGRAM COMP
COMPLEX A(310), B(93), C(310), D(93)
OPEN(3,FILE='FOO.FIL',FORM='UNFORMATTED',STATUS='REPLACE')
A = 1.0
B = 2.0
C = 3.0
D = 4.0
WRITE(3) A
WRITE(3) B
REWIND(3)
READ(3) D
READ(3) C
WRITE(*,*) C(1)
WRITE(*,*) D(1)
STOP
END
|
|
|
Back to top |
|
 |
Abhishek
Joined: 22 Dec 2010 Posts: 28
|
Posted: Sat Jan 15, 2011 2:33 pm Post subject: |
|
|
Yes REWIND is working.
But REWIND positions the file at record 1, whereas I need to do backward reading. |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|