Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

opening a file locked by MS OFFICE

chunky_lover_23
Beginner
1,408 Views

I am trying to open an ascii file which is currently opened in Excel.

Unsurpringly the program crashes. [severe (30); open failure on Unit]

Is there a compiler directive to allow the file to be opened in a read-only mode ?

I have tried the various ACTION='READ' & SHARED options in the open command.

0 Kudos
7 Replies
Steven_L_Intel1
Employee
1,408 Views
Excel would need to have opened the file with the "allow read" option. If it didn't, there's nothing you can say in Fortran to change that. Can you open the file in NOTEPAD while Excel has it open?

ACTION='READ' would be your best hope in the Fortran code.
0 Kudos
chunky_lover_23
Beginner
1,408 Views

yes notepad and word and indeed MS-DOS editor can all open the file when Excel has it opened (MS word evebn produces a fancy dialogue panel, telling me its locked and sking if I want it as Read only)

Ther must be someway of doing this in Fortran ..

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,408 Views

How about executing a shell COPY command to copy the file to a temp file. Then open the temp file. You wouldn't get an error status but you could delete the potential temp file first, then COPY, then if temp file exists assume copy succeeded.

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
1,408 Views
I tried an example but could not get the open to fail. I created a text file (t.csv), opened it with Excel and then tried opening with Fortran. I had no trouble doing so. Can you give me the steps to reproduce the problem?

The Fortran run-time automatically tries opening the file for read-only access if it fails to open for write. You could specify ACTION='READ',SHARE='DENYNONE' to be most permissive, but my tests so far show this is not required.
0 Kudos
chunky_lover_23
Beginner
1,408 Views

the code is simple:

program count_recs
c
c read in a file and see how many records it has
c
character*256 fname
character*80 line
c
fname='W:DieselEnginedsl_bw_and_w_new_w_matrices'//
& 'jmoffat_b_w_Jun 2007.csv'
c
open(12,file=fname,status='old',Action='read')
c
10 read(12,'(a)')line
write(6,*)'opened and read ',line(1:20)
close(12)
c
stop
end

I've attached a screen dump of the error.

It may be worth pointing out I',m actually using CVF v 6.1 at this point ...

0 Kudos
chunky_lover_23
Beginner
1,408 Views

Actually, this seems to cure the issue for me ..

open(12,file=fname,status='old',Action='read',
& share='denynone')

0 Kudos
Steven_L_Intel1
Employee
1,408 Views
Augh - CVF 6.1 (eight years old)! I need to start asking for versions...
0 Kudos
Reply