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

Reading Dos and Unix format text files with Fortran

nameiii
Beginner
2,292 Views
We use Fortran on WIndows (currently Compaq Fortran 6.6) and Linux (Intel Fortran 7.1) and often transfer files between the two systems.
For source files there is never any problem. However for formatted (ascii text) files which are read into the program it is important to ensure that they are in the correct format (i.e. dos/windows or unix format - the difference is I think to do withwhether carriage return or line feed or both mark the end of each line).
Does anyone know if there is an easy way to read either format without having to know what the format is beforehand? This would be a very useful option for us.
Many thanks for any help.
0 Kudos
6 Replies
nameiii
Beginner
2,292 Views
I'm replying in the hope of generating some more interest in this topic -hopefully its acceptable to reply to one's own message!
An investigation suggests there is no problem with Compaq Fortran on Windows, only with Intel Fortran on Linux (no results - yet - for Intel on Windows).
Most editors edit both dos and unix format ascii text files interchangeably (windows notepad is the only exception I'm aware of), so one can have two files which look identical on screen, but one can be read by Fortran but the other can't. Also if one emails input files between dos and unix machines as an attachment, the result may or may not be readable by fortran, depending on how the email handles the .txt file.
Since even the compiler regards the two formats as interchangeable for source files, I would have thought it would be good if they could be read interchangeably as input files to a program.
Does anyone know if there is an easy way to do this (short of searching for carriage returns and line feeds in the file and then closing and reopening with the appropriate 'RECORDTYPE = ...')?
Also, do others feel this would be useful? (I guess the lack of responses to my previous post suggests no, but I live in hope!). If so, and if there's no easy way at present that I'm unaware of, is it worth suggesting to Intel as a possible improvement?
Dave
0 Kudos
Steven_L_Intel1
Employee
2,292 Views
In my own experience, DOS-formatted files (with CR-LF terminators) are not acceptable to many Linux/UNIX utilities. Fortran I/O believes that it is reading files it would have written, using the OS-dependent convention for line terminators. Given that on Linux the convention is a single LF for a terminator, how is the Fortran I/O system supposed to know that the CR isn't intended as data?

Intel Fortran does support an option that does what you want - open the file with RECORDTYPE='STREAM_LF'. If you do this, it will assume that the file has CR-LF record delimiters. Does this help?
0 Kudos
nameiii
Beginner
2,292 Views
Many thanks for your fast response.
I agree the CR could be data, so it would have to be an option. STREAM_LF, and STREAM_CRare helpful, but one still (I think - I confess I haven't tested it) needs to know beforehand which format one has. (Also did you mean STREAM_CR not STREAM_LF? - I assumed STREAM_LF means a single LF terminator)
Perhaps we are unusual in working ina mixed Linux/Windows environment and ending up with dos files on Linux platforms and vice versa. However when the problem occurs, and the input file'looks' to be in the correct format it can take a long time to realise what the problem is.
We will probably end up scanning the first record forCR (after its been read by fortran, so the operating system's LF is removed) and warn the user if the format is wrong.Hopefully that will work?
0 Kudos
Steven_L_Intel1
Employee
2,292 Views
No, I meant STREAM_LF. Yes, you would expect that STREAM_LF looks for a single LF, but if you read the documentation, it says it looks for CR-LF. I think that when this got implemented for DVF years ago, they got it wrong and the error propagated. Back on VAX/VMS, when this option first appeared, it really did mean LF-only, and STREAM meant CR-LF. But in this implementation, STREAM is no delimiter at all (supposedly.)

You could open the file FORM='BINARY', read 512 bytes or so, and scan to see if a LF was preceded by a CR, close and then reopen the file.
0 Kudos
nameiii
Beginner
2,292 Views
Thanks - that's very useful info. The only Linux Intel Fortran doumentation I'm aware of which mentions this is the programmers reference (my version is FWL-710-02). This (p 10-140) says STREAM_LF means terminated with LF (and similarly for CR) and that STREAM_LF is the default (for formatted sequntial access).
If the default is a single LF, does this mean there is no RECORDTYPE to indicate this or does STREAM_CR indicate this? I've never come across a single CR to terminate files, but I guess some systems may do this.
0 Kudos
Steven_L_Intel1
Employee
2,292 Views
It is confusing, and the manuals do seem to be contradictory.

Anyway, STREAM_LF is what you want. I've tested it. I'm unsure how to read with just an LF on Windows, but maybe it "just works" by default on Windows.
0 Kudos
Reply