Fortran files in Microsoft Visual Studio

In summary: 240.0000 11.00000 269.0000 11.50000 ... 300.0000 12.00000 319.0000 12.50000 ... 340.0000 13.00000 359.0000 13.50000 ... 380.0000 14.00000 389.0000 14.50000
  • #1
Rositta
13
0
I'm new to Microsoft Visual Studio (2010) & I'm a bit confused by the way Visual Studio handles fortran files. I saved a set of data in one console and tried to read the data from the created file in another console. I receive the next message:

file opened correctly!
forrt1: severe<24>: end-of-file during read, unit 1, file C:\Doculents and settings\...

I've tried to save the data as .dat and .txt and the message remained the same. The funny thing is that I can read the saved .txt and .dat files in Matlab!

Does anyone know why I receive this message?
 
Technology news on Phys.org
  • #2
Sounds like your trying to read past the end-of-file during your input. Try using the END option within your READ statement.
 
  • #3
TheoMcCloskey said:
Sounds like your trying to read past the end-of-file during your input. Try using the END option within your READ statement.

Thanks a lot TheoMcCloskey. I have done some searching to fully understand the function of the END specifier with little success. Could you please expand a bit on the use of END?
 
  • #4
The statement

READ (10, 50, END=200) X

says: "Try to read the variable X from unit 10 using format statement 50. If you hit the end of the file while trying to read X, jump to statement 200."

In order for this to work, of course, you have to put statement number 200 on the statement that you want to pick up execution from when you reach the end of the file.
 
  • #5
jtbell said:
The statement

READ (10, 50, END=200) X

says: "Try to read the variable X from unit 10 using format statement 50. If you hit the end of the file while trying to read X, jump to statement 200."

In order for this to work, of course, you have to put statement number 200 on the statement that you want to pick up execution from when you reach the end of the file.

Thanks a lot for the quick answer.. I'll see if that will solve the problem..
 
  • #6
Well.. the file is still unread.. I think I should post the codes=>

=>The program I used to create the data is

program xytable
! this code is to create data for the interpolation code written in console12
implicit none
integer::i,j
integer,parameter::m=50
real,dimension(1:m)::t,h
real,dimension(m,2)::A ! a 2D matrix to store x & y


!create data
do i=1,m
t(i)=(i*1.-1.)/2.
h(i)=f(t(i))
A(i,1)=t(i)
A(i,2)=h(i)
end do

!save data
open(1,file='xytable.txt')
do i=1,m
print*,(A(i,j),j=1,2)
write(1,*)(A(i,j),j=1,2)
end do
close(1)



!define function f(x)
contains
function f(t)
real::f,t
f=2*t**2-t
end function f

end program xytableThe data as appear in the text file are:

0.0000000E+00 0.0000000E+00
0.5000000 0.0000000E+00
1.000000 1.000000
1.500000 3.000000
2.000000 6.000000
2.500000 10.00000
3.000000 15.00000
3.500000 21.00000
4.000000 28.00000
4.500000 36.00000
5.000000 45.00000
5.500000 55.00000
6.000000 66.00000
6.500000 78.00000
7.000000 91.00000
7.500000 105.0000
8.000000 120.0000
8.500000 136.0000
9.000000 153.0000
9.500000 171.0000
10.00000 190.0000
10.50000 210.0000
11.00000 231.0000
11.50000 253.0000
12.00000 276.0000
12.50000 300.0000
13.00000 325.0000
13.50000 351.0000
14.00000 378.0000
14.50000 406.0000
15.00000 435.0000
15.50000 465.0000
16.00000 496.0000
16.50000 528.0000
17.00000 561.0000
17.50000 595.0000
18.00000 630.0000
18.50000 666.0000
19.00000 703.0000
19.50000 741.0000
20.00000 780.0000
20.50000 820.0000
21.00000 861.0000
21.50000 903.0000
22.00000 946.0000
22.50000 990.0000
23.00000 1035.000
23.50000 1081.000
24.00000 1128.000
24.50000 1176.000

=>The program I am reading the data in is:

program readtext
implicit none

!interface between main program and subroutine
interface
subroutine polint(x,y,xin,yout,dyout)
use nrtype
real(sp),dimension(:),intent(in)::x,y
real(sp),intent(in)::xin
real(sp),intent(out)::yout,dyout
end subroutine polint
end interface

!declare variables
real,allocatable,dimension(:)::x,y
real::xin,yout,dyout,t,h
integer::p,st,row,col
integer,parameter::n=50
real,dimension(n,2)::A


!make memory for the input x and y arrays
if(allocated(x)) then
deallocate(x)
end if
if(allocated(y))then
deallocate(y)
end if
allocate(x(1:n),y(1:n),stat=p)
if(p /= 0) then
print*,"allocation error"
stop
end if


!load data into the code
open(unit=1,file='xytable.txt',form='formatted',iostat=st)
if(st/=0)then
print*,'Error opening file. File load status=',st
stop
else
print*,'file opened correctly!.ios=',st
end if

do row = 1,n
read(1,*,end=50)(A(row,col),col=1,2)
end do

50 do row = 1,n
print*,(A(row,col),col=1,2)
end do

print*, "dimension of x and y arrays="
print*,n

end program readtext

I reach the statement '50' but all the numbers in both columns are zeros. I've been advised to change the read loop to:

read(1,*,end=50) A(row,:)
read(1,*,end=50) A(row,1),A(row,2)

but the result is unchanged.. I really can't see where the problem lies.
 
  • #7
Rositta,

I was able to sucessfully run this (without interface to polint) but I had to make one change in your open statement within READTEXT program; change "io stat" to "iostat".

I used gfortran (gcc ver 4.3 20070522) on Windows XP.
 
  • #8
TheoMcCloskey said:
Rositta,

I was able to sucessfully run this (without interface to polint) but I had to make one change in your open statement within READTEXT program; change "io stat" to "iostat".

I used gfortran (gcc ver 4.3 20070522) on Windows XP.

Thanks TheoMcCloskey. It is "iostat" in my original code. Apparently I made a mistake during pasting the code. I still have the problem of getting zero valued entities instead of the saved numbers.
 
  • #9
I'm not sure what else to say. Your program "XYTABLE" does produce the required 50 lines of data, 2 per line, no delimiter, spaced separated.

The very fact that your original problem suggested reading past the end-of-file (EOF) has me suspicious. I did not have that problem. The program should not require the read statement “END” option in this particular case. I’ve modified the program “READTEXT” as shown below to print a message if, in fact, it does try to read past EOF (see listing below). Try it and let me know.

Are you sure you are connected to data file? What does your print message flag report (variable “st”).

Code:
 [FONT="Courier New"]
      program readtext
      implicit none
      
      !interface between main program and subroutine
!     interface
!     subroutine polint(x,y,xin,yout,dyout)
!     use nrtype
!     real(sp),dimension(:),intent(in)::x,y
!     real(sp),intent(in)::xin
!     real(sp),intent(out)::yout,dyout
!     end subroutine polint
!     end interface
      
      !declare variables
      real,allocatable,dimension(:)::x,y
      real::xin,yout,dyout,t,h
      integer::p,st,row,col
      integer,parameter::n=50
      real,dimension(n,2)::A

      
      !make memory for the input x and y arrays
      if(allocated(x)) then
      deallocate(x)
      end if
      if(allocated(y))then
      deallocate(y)
      end if
      allocate(x(1:n),y(1:n),stat=p)
      if(p /= 0) then
      print*,"allocation error"
      stop
      end if
      

      !load data into the code
      open(unit=1,file='xytable.txt',form='formatted',iostat=st)
      if(st/=0)then
          print*,'Error opening file. File load status=',st
          stop
      else
          print*,'file opened correctly!.ios=',st
      end if

      do row = 1,n
          read(1,*,end=50)(A(row,col),col=1,2)
      end do
      go to 60

   50 continue
      print *, "WARNING: read past EOF"

   60 continue
      do row = 1,n
          print *,(A(row,col),col=1,2)
      end do

      print *, "dimension of x and y arrays="
      print *,n
      
      
!     pause
      end program readtext[/FONT]
 
  • #10
TheoMcCloskey said:
I'm not sure what else to say. Your program "XYTABLE" does produce the required 50 lines of data, 2 per line, no delimiter, spaced separated.

The very fact that your original problem suggested reading past the end-of-file (EOF) has me suspicious. I did not have that problem. The program should not require the read statement “END” option in this particular case. I’ve modified the program “READTEXT” as shown below to print a message if, in fact, it does try to read past EOF (see listing below). Try it and let me know.

Are you sure you are connected to data file? What does your print message flag report (variable “st”).

Code:
 [FONT="Courier New"]
      program readtext
      implicit none
      
      !interface between main program and subroutine
!     interface
!     subroutine polint(x,y,xin,yout,dyout)
!     use nrtype
!     real(sp),dimension(:),intent(in)::x,y
!     real(sp),intent(in)::xin
!     real(sp),intent(out)::yout,dyout
!     end subroutine polint
!     end interface
      
      !declare variables
      real,allocatable,dimension(:)::x,y
      real::xin,yout,dyout,t,h
      integer::p,st,row,col
      integer,parameter::n=50
      real,dimension(n,2)::A

      
      !make memory for the input x and y arrays
      if(allocated(x)) then
      deallocate(x)
      end if
      if(allocated(y))then
      deallocate(y)
      end if
      allocate(x(1:n),y(1:n),stat=p)
      if(p /= 0) then
      print*,"allocation error"
      stop
      end if
      

      !load data into the code
      open(unit=1,file='xytable.txt',form='formatted',iostat=st)
      if(st/=0)then
          print*,'Error opening file. File load status=',st
          stop
      else
          print*,'file opened correctly!.ios=',st
      end if

      do row = 1,n
          read(1,*,end=50)(A(row,col),col=1,2)
      end do
      go to 60

   50 continue
      print *, "WARNING: read past EOF"

   60 continue
      do row = 1,n
          print *,(A(row,col),col=1,2)
      end do

      print *, "dimension of x and y arrays="
      print *,n
      
      
!     pause
      end program readtext[/FONT]

Hi TheoMcCloskey.. I've tried the modification with no change in the output. I do have a data file which I can read in Matlab as I mentioned in my first post. The flag report gives st=0. I found another Physics Forum thread talking about the same problem:
https://www.physicsforums.com/showthread.php?t=307269
Could it be a compiler problem?
 
  • #11
well.. the code worked with no problems at all in gfortran..
 

1. What is Fortran?

Fortran is a high-level programming language used primarily for scientific and numerical computing. It was developed in the 1950s and is still widely used today in fields such as aerospace, engineering, and weather forecasting.

2. Can I use Fortran files in Microsoft Visual Studio?

Yes, it is possible to use Fortran files in Microsoft Visual Studio. However, Visual Studio does not have built-in support for Fortran, so you will need to install a Fortran compiler and integrate it with Visual Studio in order to compile and run Fortran code.

3. How do I compile Fortran code in Visual Studio?

In order to compile Fortran code in Visual Studio, you will need to first install a Fortran compiler, such as Intel Fortran or GNU Fortran. Then, you can add a Fortran project to your Visual Studio solution and configure it to use the Fortran compiler. From there, you can build and run your Fortran code within Visual Studio.

4. Are there any limitations to using Fortran files in Visual Studio?

There are some limitations to using Fortran files in Visual Studio, as it is not a dedicated Fortran development environment. Some features, such as debugging and code completion, may not be available for Fortran code in Visual Studio. Additionally, not all Fortran compilers may be compatible with Visual Studio.

5. Is Fortran the best language for scientific computing in Visual Studio?

This ultimately depends on personal preference and the specific needs of your project. While Fortran is a popular language for scientific computing, Visual Studio also supports other languages such as C++, C#, and Python, which may be better suited for certain tasks. It is recommended to research and compare different languages before choosing one for your project in Visual Studio.

Similar threads

  • Programming and Computer Science
Replies
5
Views
185
  • Programming and Computer Science
Replies
2
Views
169
  • Programming and Computer Science
Replies
22
Views
870
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top