Use of Inquire to find Next record

I am writing a program (DBMAIN) to add records to my direct access database named DBPP.
I want to get the last record number so that the next record I add is recno= n+1
Upon Compiling I keep getting the error “missing name”
I have a zipped files with the code and file but I can’t figure out how to attach it to this message?
Floyd

Unfortunately, that is not possible, but since it is a compile-time error, why not show the code and the actual error message? Is this in the INQUIRE statement?

I thought we were not supposed to put code in this message but rather attach it as a zipped file??
But here is a small fragment
OPEN(UNIT=10,FILE=‘DBPP’,ACCESS=‘DIRECT’,STATUS=‘OLD’,RECL=160,FORM=‘UNFORMATTED’)
INQUIRE(UNIT=10,EXIST=‘FILE_EXISTANCE’,OPENED=‘OPEN_STATUS’,NEXTREC=‘NEXREC’)
PRINT *,‘THE FILE EXISTS?=’,FILE_EXISTANCE
PRINT *,‘THE FILE IS OPENED?=’,OPEN_STATUS
PRINT *,'THE NEXT RECORD IS ',NEXREC
The error message is:
Compiling program unit DBMAIN at line 1:
1110-S: “DBMAIN.f90”, line 46, column 25: Missing name.
Encountered 1 error, 0 warnings in file DBMAIN.f90.

The INQUIRE statement should be

INQUIRE(UNIT=10,EXIST=FILE_EXISTANCE,OPENED=OPEN_STATUS,NEXTREC=NEXREC)

where FILE_EXISTANCE and OPEN_STATUS are logical variables and NEXTREC is an integer variable.

Note that each specifier is in one of two forms:

  • Keyword = expression
  • Keyword = variable

The first form is for supplying known values to the Fortran processor, and the second form is for receiving result values from the Fortran processor.

You can safely include code in your posts. You can make it stick out via the </> symbol in the toolbar. I guess the restriction is not to put tons (metric or otherwise) of code into a post :slight_smile:

You can also post short snippets preceded by ```fortran and followed by ``` and get
automatic highlighting as well

program show
implicit none
logical :: file_existance, open_status, ready
integer :: nexrec, ios
character(len=256) :: message
character(len=:),allocatable :: fname
   ready=.false.
   fname='DBPP'
   open(unit=10, file=fname, access='direct',  &
     & status= 'old', recl=160,form='unformatted', iostat=ios, iomsg=message)
   if(ios.eq.0)then
      inquire(unit=10, exist=file_existance, opened=open_status, nextrec=nexrec)
      print *, 'the file '//fname//' exists?= ', file_existance
      if(file_existance)then
         print *, 'the file is opened?=',  open_status
         if(open_status)then
            print *,'the next record is ', nexrec
            ready=.true.
         endif
      endif
   else
       print *, '<ERROR>'//trim(message)
   endif
   if(.not.ready)stop 1
   print *, 'ready'
end program show

Note that it varies between compilers but if file existence is false then other values are likely to be undefined, so the above conditional testing is not as excessive as it might appear.

Also note since the original post did an OPEN() with STATUS=‘OLD’ the program would have failed there if the file did not exist or did not open, so those tests are a bit redundant. Using the IOSTAT and IOMSG parameters on OPEN would give you the simpler:

program show
implicit none
integer :: nexrec, ios, lun
character(len=256) :: message
   open(newunit=lun, file='DBPP', access='direct',  &
   & status= 'old', recl=160,form='unformatted', &
   & iostat=ios, iomsg=message)
   if(ios.eq.0)then
      inquire(unit=lun, nextrec=nexrec)
      print *,'the next record is ', nexrec
   else
      print *, '<ERROR>'//trim(message)
      stop 1
   endif
   print *, 'ready'
end program show

OK, when I changed the specifiers as you suggested, the compile error went away and it compliled.
However, I got an unresolved external error.
DBMAIN.obj : error LNK2001: unresolved external symbol label
DBMAIN.exe : fatal error LNK1120: 1 unresolved externals
I don’t have any modules or subroutines so I think it is having trouble finding the ‘DBPP’ file.
I read that some of the file names are system dependent? Any ideas?
Floyd

No point in trying to fix problems without seeing the source code as well as the error messages. There is nothing in the fragment of code that you have shown to match “label” as an external symbol.

Please upload your Fortran source file to a cloud service such as Dropbox, Google Drive, OneDrive, Github or paste.org, and provide a link here. (If the code is quite short, say 50 lines, you can paste it into your reply using the “</>” button.)

I fixed the previous problem and now the program compiles and runs. However, the program is not reading the next record number from the INQUIRE statement on line 46. I know the file DBPP contains 25 records but the program says nextrec is 1. It doesn’t see any records? What am I missing?

Floyd

image001.jpg

(Attachment DBPP is missing)

(Attachment DBMAIN.F90 is missing)

NEXTREC = 1 is correct. If no record has been read or written, the value assigned is 1.

What do you want to do to the file once it is opened?

It should have been nextrec=26 because I built the file by reading 25 rows of data from a text file. I want to add records to the file using this new program I am trying to write. I would send you the files but the moderator rejects them??
Floyd

This forum does not have a provision for attaching files to posts. That rule does not depend on a moderator.

To attach files, please follow the procedure that you used in your previous thread, Reading data into direct access files - #37 by miramar46 .

The effect of a NEXTREC = clause in an INQUIRE statement depends on what has been done with the file earlier in the program. Here is the wording from the 2018 standard:

The scalar-int-variable in the NEXTREC= specifier is assigned the value n + 1, where n is the record number of the last record read from or written to the connection for direct access. If there is a connection but no records have been read or written since the connection, the scalar-int-variable is assigned the value 1. If there is no connection, the connection is not for direct access, or the position is indeterminate because of a previous error condition, the scalar-int-variable becomes undefined.

Perhaps, you misunderstand the role of the NEXTREC= clause in an INQUIRE statement. It does not set the record number for the subsequent READ/WRITE statement; it gets the anticipated record number; the variable after the ‘=’ can be undefined, or have any value whatsoever, prior to executing the INQUIRE statement; when the INQUIRE is executed, that value will be overwritten by the record number that will be used by the next READ/WRITE statement.

I guess I do not understand. I created the file ‘dbpp’ in another program. It has 25 records. Now I am writing a new program called DBMAIN where I want to add some more records from the keyboard. I assume that the next record should be 26 , since there are currently 25 records in the file.
It sounds like the program must read from the file in order to get the nextrec. But I thought that is what the INQUIRE statement is for. Please see my latest version in the attached link:
https://drive.google.com/file/d/1yd7mCFojuFt2PKgyeZxIjCWRCMyGc1eA/view?usp=sharing
Floyd

You have not given the public read access to the file on your Google Drive.

I thought I had? Try this one.
https://drive.google.com/file/d/1yd7mCFojuFt2PKgyeZxIjCWRCMyGc1eA/view?usp=sharing

OK, now I am able to download the Zip file. However, the file PPDB in it is empty.

As I wrote above, the file PPDB is empty.

Please do not make arbitrary assumptions regarding how Fortran file operations work. Instead, read relevant documentation on statements such as OPEN, INQUIRE, etc.

Just after a file is opened, information regarding how many records are present in the file, the file size, etc., may not be available even in the RTL. You cannot expect a Fortran program that you run today to know what a different Fortran program did when you ran the latter last week.

Sorry, I copied the wrong file. This one is 4KB

After opening a file, the position is the beginning of the file, and thus the next record to be read/written is 1. The inquire statement is functioning correctly here. It sounds like what you want is for the file to be positioned at the end, so you can append more data to it. Try adding position = 'APPEND' to your open statement.

That specifier is not applicable to files opened for direct access. It is permitted for stream or sequential access files.

I was afraid of that. I don’t use direct access files very often.