Topic: I/O Help: Understanding how to set up data Files

I would like to have my Fortran program open and read data files, then write back to files at the end of the program. I have written a simple program to open and read a data file that is in the same top level directory as the build and modules folders. I have no idea how to create a data file. I tried to use notepad to create a vertical list of numbers (each number on a new line) with no commas or punctuation. I called the file read1.DAT, changing the .txt extension to a .DAT

I have written the program to open the file and return a value for IOSTAT. If the file opens, IOSTAT=0 and the reading process is supposed to begin. 

The first time I did this, I tried to open the read1.DAT file. . This was not successful, returning the error message "an error occurred reading line 1" which indicated that the file was opened but could not be read.

I then created an identical file and called it read1.txt. This time I received a message saying that there was an error opening the file, IOSTAT=2 (I don't know what this signifies except that the file was not opened properly).

Please help me understand how to set up files that contain data that can be read by my Simply Fortran code. I'm not sure if the files have to be labeled ".DAT" or if there are any rules on how to set them up so the program can read them. Any help would be gratefully received.

Regards

Euan

2 (edited by JohnWasilewski 2012-05-25 22:31:24)

Re: I/O Help: Understanding how to set up data Files

If I had a text file containing these two lines
SOME TEXT (20 chars)
      10      11      12   3.143    2.54     1.5

I'd expect a program containing the following code to read them into variables StringA, I,J,K, X,Y,Z


C
C
C
      CHARACTER FiNAM*12, StringA*20

C     Provide some code to :
C       - prompt user for a filename in FiNAM
C       - assign a value to LUI (eg 7)
C       - assign a value to LUO (eg 8)

         OPEN(UNIT=LUI,
     +        FILE=FiNAM,
     +        STATUS='OLD',
     +        ERR=50,
     +        IOSTAT=IOCODE )

C        No ERR, so say so
         WRITE(LUO,'('' Opened.'')')

      READ(LUI, '(A)') StringA
      READ(LUI, '(3I8,3F8.3)') I,J,K, X,Y,Z
C
C
C



50       CONTINUE
C        Error-handling
         WRITE(LUS,'(''Error code ['',I4,''].'')') IOCODE


-----
John

Re: I/O Help: Understanding how to set up data Files

John,

Thanks for the reply and the code which I believe I understand....before I test this, could you confirm the unit number for input and output should NOT be 5 or 6 (designating keyboard and screen) and that the data file should be stored in the same directory as the project?

Regards

Euan

4 (edited by JohnWasilewski 2012-05-25 22:33:24)

Re: I/O Help: Understanding how to set up data Files

Yes, for the v simple example I gave, I agree with that.  I've corrected my previous post as you say.  The advantage of using 'LUI' and 'LUO' is that you can set them to 5 and 6 if you want screen input and output, without having to have different FORMAT statements, depending on where the I/O reads and writes.

If you want to have the program executable and data + results files in different parts of teh directory structure, you'll need to use use pathnames instead of filenames, in which case the filemane variable will need to be much larger.

I gave this example to try to help you get started but I am no longer using this method of opening files.  I'm using DISLIN instead, to create a GUI and use Windows filename requesters (dialog boxes). 

This is what a Fortran statement looks like to open a file by the DISLIN GUI method (this sends a dialog box to the screen with the prompt shown in the CALL statement):

      CALL dwgfil('INPUT file name', InF%NAM, '*.*')

I suggest, though, that you get the hang of using a DOS interface before having a crack at the DISLIN method.

Good luck with it.
--
John