Reading data into direct access files

That program runs fine on my compiler, if you add an END statement to end the program unit.

The next thing to try is include an IOSTAT=icode in specifier in the READ statement and look up in your compiler’s docmumentation the meaning of the value of icode.

As @themos wrote, the program that you showed in your latest post works fine after appending an END statement. I checked and confirmed that both LF71 and Gfortran produce the same output file (byte-for-byte).

In order to overcome problems with exchanging source and data files, in the future you should use a file hosting service. It is best if you zip up all pertinent files (source, data, instructions to build and run), upload the Zip file to the file host, enable read/share access to the file,and provide a link to the shared Zip file.

Pasting code or other text in your posts here, as you have been doing, will often not work correctly, since invisible characters such as space, tab, etc., may get changed during the paste and copy process.

It will be easier for readers to understand your problem descriptions if you use more correct terminology. For instance, consider the title of this thread. One does not “read data into a file”. We can read data from a file, or we can write data to a file, and the data may be modified while it is in the fast memory of the computer, between the read step and the write step.

Similarly, “53 spaces per row” is misleading, if what is meant is “53 characters per line”. To programmers, when file contents are being described, “space” means a specific character – most commonly, the ASCII character with code Z’20’, which corresponds to the space-bar on a keyboard.

I zipped the files and put them on Google Drive. Use this link: https://drive.google.com/file/d/1rjG4RDOggcjOunhW1WUsycMszwhQn7Hx/view?usp=sharing

I have never done this before, so I am not sure if I did it right. Let me know if you get this

Floyd Pfeffer

image001.jpg

@miramar46, we are “almost there”. I am able to reach the file location from your link, but I lack permission to open/download the file.

Before giving the public access to your uploaded file, assure yourself that you have the right to publish/share the file with the public.

You probably forgot to change the permission for the Zip file from the default to “allow anyone with the link to view/download”. You can go back into your GoogleDrive account, select the Zip file in question, right click, Get Link, and change the permission from “Restrict” to “Anyone with the link”. After doing so, obtain a link and post the new link.

Here’s the new link: https://drive.google.com/file/d/1rjG4RDOggcjOunhW1WUsycMszwhQn7Hx/view?usp=sharing

Hope this works.

Floyd

image001.jpg

1 Like

Good work!

I downloaded and unzipped the Zip file, and changed the name of the input data file to match the name used in the OPEN statement in your program (by removing the “.TXT” suffix). The program ran normally after being compiled using LF 7.1, and produced the unformatted direct access file as expected.

1 Like

Fortran has some strange habits. I went and looked at the data file and it did not have .txt in the filename. Only listed by the operating system as a text file. So I went into my code and added .txt to the file name and it worked!!

So help me understand something. The error message “end of file reached” had nothing to do with the problem. The problem was the compiler wasn’t reading the file? Correct?

Thanks again,

Floyd

image001.jpg

Yes Fortran has lots of quirks, just like us old folks!

Check if you have more than one copy of the input data file, possibly in different directories, with slightly different names.

Here is a more complete error report from your program when it is run with the input data file not named to agree with the name in the OPEN statement.

 ERROR OPENING FILE PPDATA
jwe0021i-s line 22 An endfile record was detected in a READ statement (unit=10).
 error occurs at _MAIN__  line 22 loc 004010c6 offset 000000c6
 _MAIN__      at loc 00401000 called from o.s.

There are two errors here. The first is that the OPEN statement failed, and the first line in the above printout says so. In your program, you asked for IOS to be set in the OPEN statement and printed a message when IOS /= 0, but you let the program continue to read a file that had not been correctly opened. That explains the next error message (“endfile”, etc.).

Earlier, you reported the second message, but not the first, so we did not know that the OPEN had failed, and we could not make a correct diagnosis. Somewhat similar to a patient telling the doctor, “my stomach hurts” instead of “I ate some wild mushrooms, and my stomach hurts”.

Often, if one error is ignored and the program is allowed to continue, we may see lots of confusing and cascading error messages. The program is no longer in a “sane” state after the first error.

OK, so I should put a STOP after the IOS came back positive. One final question: When I am reading unformatted data in Sequential access, all I need between each number is a space? The compiler will interpret the space as a delimiter signifying a new number (or character). Is that right?

Thanks again,

Floyd

image001.jpg

That question indicates that you do not understand what unformatted Fortran files are. I suggest that you read some books or online tutorials to learn about such files, if you really need to use such files. There are many technical rules and restrictions on their behavior, and they are not tolerant to errors in usage. You could almost say, “UNFORMATTED” = “UNFORGIVING”. If you do not know how an unformatted file was opened or use a different Fortran compiler, or you do not have the WRITE statements used to write that file and the declarations of the variables in the I/O list of that WRITE statement, it is almost impossible to recover the information in the unformatted file.

For programmers new to Fortran, it is best to use list-directed I/O, and gradually start using formatted I/O and then unformatted, stream, direct/sequential access, etc.

In the meantime, why are you interested in unformatted files at all?

I guess you are right. I do not understand unformatted files are. I have read many books and manuals but they only explain how to write the code. They assume that I know what unformatted and formatted file look like.
Can you recommend any good books on Fortran 95 that do this?

As to what I need? I am a chemical engineer. I want to store a lot of physical property data in a file on my computer. (a physical property database) Later I will write programs to retrieve data on a few of these compounds. And then the program will use this data to perform calculations and write reports. I don’t care if the data is stored so I can read it, only that I can retrieve it , for example: rec 10 HCl, rec 18 chorine and rec 24 aniline and all the data on the rows corresponding to those compounds. Sometime, I will only want to retrieve part of the data for specific compounds, like heat capacity constants, molecular weights, etc.

Floyd Pfeffer

Suppose you save the data as plain text, as you showed earlier. How long does it take to read the entire data set in that format? If it takes just a few seconds, and that is fast enough, you don’t need to use unformatted files.

Noted. Here is where it may be good to have an idea about what “a lot” is, and how you plan to retrieve some of that data later. You probably want to be able to update existing data or add new data at some future point, as well.

Are we thinking of about a hundred compounds? A thousand? A million?

How are we going to retrieve the data? By chemical name? Common name? A certain property, e.g., density, within a certain range?

These days, “database” is usually understood to be a relational database, such as Oracle, IBM-DB2, MS SQL-server, etc. These databases use many kinds of “unformatted” files, but a user never deals with the files directly. Instead, we use the SQL language or applications that connect a character or graphical UI to the SQL engine and the database.

Whether a simple file is enough or a relational database is to be used is a decision that can be made after the questions that I raised have been addressed.

Sounds like you are developing your ideas and you might think of more properties to add to the data file. In that case, forget formatted files for now. They do become useful when your program starts being I/O bound.

Briefly, the difference between formatted and unformatted is that the integer value 1 is stored in unformatted files as the bytes 1,0,0,0 (assuming a 4-byte INTEGER type) and in formatted files as the byte sequence 49, 32, 32… (corresponding to the character “1” and however many spaces you want). The unformatted bytes can be copied directly to a memory location for the integer variable that will receive them and that variable will then have the value 1. The formatted bytes need to be recognised as characters and converted to the desired integer value 1,0,0,0, which takes some time (each decimal digit character code needs to be recognised and the desired value built up with additions and multiplications).

Thanks for the input. I just wrote mecej4 and explained what I am planning to do. I assume you can see my posts too. I don’t think I am going to write anything that will be complicated enough to be slow. I wrote a program to simulate a distillation unit with several pieces of equipment. It had about 10 subroutines, each called many times and each call involved lots of iterative calculations. Despite how complex it was, when I hit the enter key to start the program, it ran in less than a second.

So I am NOT worried about speed for now.

Thanks,

Floyd

image001.jpg

No, I may have mislead you. I am familiar with large databases but my needs are much more modest. Right now I have 25 compounds, each with one integer(the record no) two Character Variables (the name and empirical formula) followed by 31 real numbers. So it is a 25 x 34 matrix now. I may have as many as fifty compounds someday. After I get this initial database built, I am going to write a program to maintain it. I want to be able to edit any record, add new records, delete records and write out the entire database to files that I can print on paper.

Floyd

I have modified my code to read the entire larger database of physical properties. Now I get an error on the fifth record. It looks like the program is looking for a real number and is seeing a character. Please see the zipped files in the attached link.

I have looked through the files in your “Invalid Decimal .zip”. Your data set is, by today’s standards, small – 25 records of 160 bytes per record, or about 4 kbytes.

There are errors in the input data file.

  1. I found a number of instances of the character with ASCII code Z’96’ where the minus sign (Z’2D’) was probably intended. In certain text editors, depending on the display font selected, Z’96’ may resemble a slightly elongated minus sign.

  2. When reading a character field using list directed input, the first blank character terminates the field. Thus, “carbon monoxide” in the input will be read as “carbon”. If the next field is expected to be a number, attempting to read a number from “monoxide” will fail with an error. One fix is to use an underscore instead of a space, “carbon_monoxide”. Or, use formatted input.

There are errors in the program. After writing the unformatted direct access file DBPP using unit number 12, you reopen the same file using unit number 10 and write formatted output to it.

Your work may be easier to manage if you separate the programs that read the unformatted direct access file from the program that creates it. You will avoid the risk of overwriting a valuable file as you have done previously because of errors in the program if, once the direct access file is created successfully, you mark it “read-only” in the OS.

The number of lines in the data file is 25. In the program, in different places you attempt to write 24, 25 or 27 lines of output.

If you include matching opening and closing ’ or " in the input, the character should be read correctly.

Thank you for your insight. I found the invalid minus signs too but I didn’t get them all corrected. The bad ones are just a little bit bigger than the valid ones. Your underscore on the two name compounds worked beautifully. I fixed the other issues you mentioned and the program works fine now.
I do not want to make the file read only yet. I am going to write another program which will edit, add and delete records. However, I will save another copy in a different folder so I have a back-up
Thanks for your help,
Floyd Pfeffer