How can I solve Fortran runtime error: End of file? - fortran

I am using Codeblock 13.12. My file is not readable. and getting the runtime error
program asd
implicit none
integer :: x
open(unit = 2, file = "text.txt")
read(2,*)x
write(*,*)x
end program
and my text.txt file is :
1
I've seen many answers, but none of them worked for my code

Your program is just fine. As a matter of fact, even if the first line is not properly terminated, gfortran will take the EOF as the EOL and still work.
Your problem is you are working in the IDE CodeBlock. The IDE does not run the program in your working directory so the file text.txt is not where the program is running, hence the end of file error.
Check out this post Codeblock working directory which will guide you to solving your problem.

Related

Fortran error: "end of file" while reading character in namelist

I am writing a Fortran application, and I get this problem. When I define a namelist as following:
CHARACTER(100) :: INPUT_DIR, OUTPUT_DIR, ROOT_DIR
NAMELIST /IODIR/ INPUT_DIR, OUTPUT_DIR
and then I read IODIR from file as:
READ(FUNIT,IODIR, ERR=99)
The data in file is:
&IODIR INPUT_DIR="Input", OUTPUT_DIR="Output" /
But it get error
"End of file".
It seems like the length of variables is longer than their defined in file. I don't know how to set delimiter for the character variable, or read an unknown character in namelist. I use GNU Fortran to build.
I had the same problem with the online gfortan compiler, same result from an installed version. This problem is all over the web, so on the basis of using reliable sources I:
Installed gfortran in Windows 10 Bash - all good.
Installed gfortran in Cygwin - all good.

invalid board header windows

I am trying to process a binary file generated by a PSI DRS4 for work. I tried this using a C++ code given in the src folder of the software used to run the board.
A friend of mine compiled and executed this code on his linux computer just fine. I wanted to adapt this code to windows. I went through the compilation step pretty easily but then when I try to execute it, it just crashes...
After some further investigation, what I understand is that the header(s) cannot be read properly (probably leading to the crash when they're supposed to be used later in the code)
I thus tried to read the file with HexEditor Neo and here is what I get: screenshot binary file Hexeditor
As you see the part after EHEADER becomes unreadable... Is it normal? Has anyone faced this situation before ?

Changing the status of a temporary file with the open statement in gfortran

I am using an old Fortran code and I have running it using g77-3.3 in an old OS. Since this version of the compiler no longer comes with most Linux distributions I tried using gfortran (4.9.2), and I am facing this small problem.
This codes uses temporary files. It writes to this file and then at a certain point it changes the status of the file to scratch, like in the following example:
PROGRAM testopenfile
IMPLICIT NONE
WRITE(8,*)'fdsasfd'
OPEN(8,STATUS='SCRATCH')
CLOSE(8)
END PROGRAM
This piece of code works with the g77 compiler, but when I run it with gfortran I get the message:
At line 4 of file testopenfile.f (unit = 8, file = 'fort.8')
Fortran runtime error: Cannot change STATUS paramter in OPEN statement.
Has this way of setting temporary files changed in gfortran? Am I doing something wrong? Could this be a bug in this version of gfortran?
If you write to unit 8 before opening the file, you are in fact writing to a file called fort.8, see here. Apparently, this file is still open at unit 8 when you try to attach the scratch file to it.
You can solve this by opening the scratch file before writing to the unit:
PROGRAM testopenfile
IMPLICIT NONE
OPEN(8,STATUS='SCRATCH')
WRITE(8,*)'fdsasfd'
CLOSE(8)
END PROGRAM
or by using a different unit.
Chapter C.6.3 ("OPEN statement (9.5.6)") in the Fortran 2008 Standard treats this case and has an example which looks just like your code. It states that this "example is invalid because it attempts to change the value of the STATUS= specifier."
If you want to delete the file on closing, you can also do
PROGRAM testopenfile
IMPLICIT NONE
WRITE(8,*)'fdsasfd'
CLOSE(8,status='DELETE')
END PROGRAM

C++ compiling and running in Sublime Text 2

I really like Sublime Text 2 for HTML/CSS and Python and I'm starting to learn C++ and I want to use Sublime Text 2. I have looked at a few tutorials on installing g++ in order to run C++ in Sublime Text 2.
This is my code:
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
And when I run it it says [Finished in 1.5s] but nothing got printed. I have added the environment variables path but nothing gets printed.
The problem is that you are pressing build, which compiles your source code into an executable but does not run it. The [Finished in ...s] you are seeing is how long the program took to compile.
What you need to do is build like you currently are, but then go to the directory where your source code is and run the executable file that's in there*. There is a run option within the editor, but it doesn't always work on Windows**.
*if the program closes instantly, try running it from the console or adding std::cin.get() to the end of your program
**often due to incorrect configuration

No output after compiling and executing the code

I'm new to fortran and i'm trying to execute this as an output.
program write2file
implicit none
! open file
open (10, file='output_file.txt', status='unknown')
! write to file
write(10, *) 'Hello World!'
! close file
close(10)
end program write2file
I'm running it on Linux and I have tried to use this statement to compile and execute the output, but unfortunately, I wasn't able to get the 'output_file.txt'
Does anyone know what's wrong?
This is a comment rather than an answer but I feel in need of better formatting. After you execute the command
ifort -o output hi.f90
you should have an executable called output in your current working directory. To execute that executable execute the command
./output
which will, if you are successful, write the output you want to wherever the current version of the program directs it.
Reading your comments makes me suspect that you think the command
ifort -o output hi.f90
ought to execute your program and cause the creation of the output requested. But that command just compiles your sources (in the file hi.f90) into the executable called ouptut. Is this the first time you've used a compiler ?