| New Reply |
FORTRAN in linux..read file problems |
Share Thread |
| Jan26-13, 09:06 AM | #1 |
|
|
FORTRAN in linux..read file problems
I wrote a simple FORTRAN program to show my read problem. This works fine in windows. On the Linux machine I always get this eof error when I open a file and read. This means that every program I have written doesn't work :) The error is line 12 EOF. What is the problem? (I am using GFORTRAN in linux)
program read_write implicit none !reads data from a file called input.dat integer :: i real a(10) !single dimension array real b(10) open(10,file='input.dat') do i = 1,10 read(10,*) a(i) !this is line 12 b(i) = a(i)**1.3 end do close(10) open(12,file='output.dat') do i = 1,10 write (12,*) a(i),b(i) end do close(12) end program read_write oh, the input file is simply: 1 2 3 4 5 6 7 8 9 10 Thanks, Jim |
| Jan26-13, 09:22 AM | #2 |
|
Mentor
|
The program may be looking for input.dat in a different directory (folder) from where it actually is. Try specifying the complete path to the file.
|
| Jan26-13, 10:24 AM | #3 |
|
|
Jim |
| Jan26-13, 10:35 AM | #4 |
|
Recognitions:
|
FORTRAN in linux..read file problems
Is output.dat a new file or one which already exists?
If it is new, I would add STATUS = 'NEW' to the open command. |
| Jan26-13, 10:43 AM | #5 |
|
|
IOW..I can always write files, I just can't read files. |
| Jan26-13, 12:22 PM | #6 |
Recognitions:
|
When you do
Code:
open(10,file='input.dat') Do a search of your file system for "input.dat" files and delete any empty (zero length) ones that should't be there. Then change the statement to Code:
open(10,file='input.dat',status='old') Usually you don't need the "status" option for an output file, because you want to overwrite an existing file if it already exists, or create a new one if it doesn't exist, and that's what happens by default. |
| Jan26-13, 03:44 PM | #7 |
|
|
I found a solution. This Linux GFORTRAN compiler handles files differently than the g77 or g95 for windows.
I added a $D,$A (CR,LF) at the end of the file. It works now. FWIW..I am using a Raspberry Pi with Debian. Thanks everyone. Jim |
| Jan27-13, 02:56 PM | #8 |
|
|
I seem to recall a problem like this. I don't remember if dos2unix/unix2dos kind of thing had something to do with it...but it certainly had something to do with the end-of-file character being right after the last entry...so, if you went to the very end and press <enter> and re-saved the file, the problem goes away.
For example, if the original file looked like this: 12.3EOL 23.4EOL 34.5EOF If wouldn't work, but if it looked like this: 12.3EOL 23.4EOL 34.5EOL EOF Then, if would work. I noticed that such condition could happen depending depending on the editor that you are using to enter the data; for example, if I use Nedit and type the data in and leave the EOF right after the last entry, Nedit automatically increases the number of line in the file and moves the EOF by itself to the next line; but, if I use something like Notepad++, it leaves the data the way I typed it. my 2 cents. |
| Jan27-13, 03:49 PM | #9 |
Recognitions:
|
It's so long since I've seen a text editor that let you create a file with an "unterminated" last line, I forgot about that problem!
But hey, one of the design goals of Unix was NOT to protect people from their own stupidity - not "what you see is what you get" but "what you got was all you deserved"
|
| Jan30-13, 04:53 PM | #10 |
|
|
Thanks for the reply. I have seen this before with different compilers. They all seem to handle CR LF differently. A few test programs were run and the solution was found.
Thanks again, Jim |
| Jan30-13, 04:55 PM | #11 |
|
|
Thanks, Jim |
| Jan30-13, 05:55 PM | #12 |
|
Mentor
|
|
| Jan31-13, 05:54 PM | #13 |
|
|
I saved the file with no "enter" at the end of the last line, and again with enter at the end. The one with no enter, has nothing after the last character. The one with "enter" has only a LF ($0A) after the last character. see attachment. (I used Leafpad in Debian Linux for this test) Thanks, Jim |
| Jan31-13, 08:58 PM | #14 |
|
|
As people develop their own editors, they can do whatever they want regardless of operating system and/or cater to both; in particular, if they are developing a multi-platform text editor....all you have to do is go to the settings and pick how you want your lines terminated: Windows or Unix style.
|
| New Reply |
Similar discussions for: FORTRAN in linux..read file problems
|
||||
| Thread | Forum | Replies | ||
| How to read Symbolic equations from a file in fortran ?? | Programming & Comp Sci | 20 | ||
| Read array from a file in Fortran | Programming & Comp Sci | 4 | ||
| Getting fortran to read a text file and write the whole file to a different location | Programming & Comp Sci | 4 | ||
| Fortran read from file problems | Programming & Comp Sci | 5 | ||
| Fortran question, read from file | Programming & Comp Sci | 5 | ||