Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read (.csv) file data into Fortran program?

4,192 views
Skip to first unread message

nik@cabana

unread,
Apr 27, 2017, 3:30:34 PM4/27/17
to
Hello all,

I am writing Fortran 77 code to extract data from .csv file into an array. Following is the code:

program xcsv
! read real numbers from CSV file
integer, parameter :: iu=20, nrows = 5, ncols = 32
real :: xx(nrows,ncols)
integer :: i, a, b
open (unit=iu,file="a.csv",action="read",status="replace")
do i=1,nrows
read (iu,*) xx(i,:)
end do
write (*,*) 'The stored value is:'
write (*,*) xx(1,1)
write (*,*) 'Enter the values for a & b:'
read (*,*) a,b
end program xcsv


But somehow it is giving me following errors, as follows:
'xcsv.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dbghelp.dll'. Cannot find or open the PDB file.
xcsv.exe has triggered a breakpoint.

The program '[10912] xcsv.exe' has exited with code 38 (0x26).

Can anybody please provide corrections to this code so that these errors can be eliminated?

Thank you in advance,

Nik

Beliavsky

unread,
Apr 27, 2017, 4:05:17 PM4/27/17
to
On Thursday, April 27, 2017 at 3:30:34 PM UTC-4, nik@cabana wrote:
> Hello all,
>
> I am writing Fortran 77 code to extract data from .csv file into an array. Following is the code:
>
> program xcsv
> ! read real numbers from CSV file
> integer, parameter :: iu=20, nrows = 5, ncols = 32
> real :: xx(nrows,ncols)
> integer :: i, a, b
> open (unit=iu,file="a.csv",action="read",status="replace")
> do i=1,nrows
> read (iu,*) xx(i,:)
> end do
> write (*,*) 'The stored value is:'
> write (*,*) xx(1,1)
> write (*,*) 'Enter the values for a & b:'
> read (*,*) a,b
> end program xcsv

When I open a file for reading I use status="old". Can you try that? Does your file have 32 numbers to read in each line?

dpb

unread,
Apr 27, 2017, 4:41:41 PM4/27/17
to
On 04/27/2017 2:30 PM, nik@cabana wrote:
> Hello all,
>
> I am writing Fortran 77 code to extract data from .csv file into an array. Following is the code:
>
> program xcsv
> ! read real numbers from CSV file
> integer, parameter :: iu=20, nrows = 5, ncols = 32
> real :: xx(nrows,ncols)
> integer :: i, a, b
> open (unit=iu,file="a.csv",action="read",status="replace")
...

> But somehow it is giving me following errors, as follows:
> 'xcsv.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dbghelp.dll'. Cannot find or open the PDB file.
...

As Beliavsky hints, status="replace" will delete any existing file of
that name and open an new one which will then be empty. Use 'old' or
simply leave STATUS off; it's unneeded here.

After that, other than using fixed constants internally, looks like
should work ok...but the input file will definitely need to have the
exact shape.

--

nik@cabana

unread,
Apr 27, 2017, 5:39:42 PM4/27/17
to
Hello Beliavsky,
Thanks for the reply! Your suggestion worked very well. Now I have following issue. I have created one program (AREA) which call function xcsv to read data from csv file. Following is the code:

PROGRAM Area
!---------------------------------------------------------------------
!
! This program gets data from stored .csv file
!
!---------------------------------------------------------------------
IMPLICIT NONE

INTERFACE
FUNCTION xcsv ()
integer, parameter :: iu=20, nrows = 5, ncols = 32
real:: xcsv(nrows,ncols)
END FUNCTION xcsv
END INTERFACE

! Declare local variables
INTEGER :: a,b
INTEGER, parameter :: NROWS=5, NCOLS=32
REAL :: radius, output(NROWS,NCOLS)
output=xcsv()

write(*, '(A)', ADVANCE = "NO") "The stored data is: "
write(*,*) output
write (*,*) 'Enter the values for a & b:'
read (*,*) a,b

END PROGRAM Area

!-----Reads data from csv file----------------------------------------------------
FUNCTION xcsv
IMPLICIT NONE
! read real numbers from CSV file
integer, parameter :: iu=20, nrows = 5, ncols = 32
real :: xx(nrows,ncols), xcsv(nrows,ncols)
integer :: i, a, b
open (unit=iu,file="a.csv",action="read",status="old")
do i=1,nrows
read (iu,*) xx(i,:)
end do
xcsv=xx
END FUNCTION xcsv


When I run this code, I am getting two errors:
1. Error 1 general error c101008d: Failed to write the updated manifest to the resource of file "C:\Users\nikhildlondhe\Documents\Visual Studio 2013\Projects\Frotran_Learning_1\Frotran_Learning_1\Debug\xcsv.exe". The operation failed. mt.exe

2. xcsv.exe has triggered a breakpoint.

How can I stop receiving these two errors??

Because Progam area will run iteratively by Abaqus software and I don't want compiler to stop it in between.

Any input is appreciated,

Than you,

Nik

jfh

unread,
Apr 27, 2017, 6:24:24 PM4/27/17
to
That program is not Fortran 77 as nik claimed. Integer,parameter and :: and end program are all later Fortran features. Does nik want a Fortran 77 or modern Fortran program?

Beliavsky

unread,
Apr 27, 2017, 7:25:23 PM4/27/17
to
When I put parentheses after FUNCTION xcsv the program can be compiled with Intel Fortran, gfortran, or g95. Can you compile the following? I don't know if you have a general Fortran problem or one specific to compiling with ifort in Visual Studio, in which case you should ask your question at the Intel Fortran forum.

!-----Reads data from csv file----------------------------------------------------
FUNCTION xcsv()
IMPLICIT NONE
! read real numbers from CSV file
integer, parameter :: iu=20, nrows = 5, ncols = 32
real :: xx(nrows,ncols), xcsv(nrows,ncols)
integer :: i, a, b
open (unit=iu,file="a.csv",action="read",status="old")
do i=1,nrows
read (iu,*) xx(i,:)
end do
xcsv=xx
END FUNCTION xcsv

nik@cabana

unread,
Apr 27, 2017, 10:19:59 PM4/27/17
to
Yes, it worked. Now when I increase the dimensions of the array which I am extracting from .csv file then I am getting fatal error. Details are as follows:

Code:

PROGRAM Area
!---------------------------------------------------------------------
!
! This program gets data from stored .csv file
!
!---------------------------------------------------------------------
IMPLICIT NONE

INTERFACE
FUNCTION xcsv()
integer, parameter :: iu=20, nrows = 81000, ncols = 11
real:: xcsv(nrows,ncols)
END FUNCTION xcsv
END INTERFACE

! Declare local variables
INTEGER :: a,b
INTEGER, parameter :: NROWS=81000, NCOLS=11
REAL :: radius, output(NROWS,NCOLS)
output=xcsv()

write(*, '(A)', ADVANCE = "NO") "The stored data is: "
write(*,*) output(1,10)
write (*,*) 'Enter the values for a & b:'
read (*,*) a,b

END PROGRAM Area

!-----Reads data from csv file----------------------------------------------------
FUNCTION xcsv()
IMPLICIT NONE
! read real numbers from CSV file
integer, parameter :: iu=20, nrows = 81000, ncols = 11
real :: xx(nrows,ncols), xcsv(nrows,ncols)
integer :: i, a, b
open (unit=iu,file="testdata.csv",action="read",status="old")
do i=1,nrows
read (iu,*) xx(i,:)
end do
xcsv=xx
END FUNCTION xcsv


The actual size of the data in excel sheet is 81000X11. It is in .csv format. I am getting following error:
Debug Assertion Failed!
Program:
...Projects\Frotran_Learning_1\Frotran_Learning_1\Debug\xcsv.exe
File: f:\dd\vctools\crt\crtw32\misc\wingsig.c
Line:418
Expression: ("Invalid signal or error",0)
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)

In command window, I am getting following message:
forrtl: severe (170): Program Exception-Stack overflow

Any idea, how to debug this problem?

Is there any limitation on the size of the data that can be imported in fortran arrays from excel sheet?

campbel...@gmail.com

unread,
Apr 28, 2017, 1:28:41 AM4/28/17
to
You are getting a stack overflow due to the use of xx and the array function return. Why not try a SUBROUTINE approach that requires fewer temporary arrays; like:
Subroutine Read_xcsv (xcsv)
IMPLICIT NONE
! read real numbers from CSV file

integer, parameter :: iu=20, nrows = 81000, ncols = 11

real :: xcsv(nrows,ncols)
integer :: i

open (unit=iu,file="testdata.csv",action="read",status="old")

do i=1,nrows
read (iu,*) xcsv(i,:)
end do
close (unit=iu)
END Subroutine Read_xcsv

I would recommend that you include some error testing, using IOSTAT= and testing for possible non-zero response.

.csv files tend to use a "," separator which can be a problem.

You could also put output or xcsv into a MODULE (or ALLOCATE) to also avoid large local arrays.

Louis Krupp

unread,
Apr 28, 2017, 1:44:45 AM4/28/17
to
On Thu, 27 Apr 2017 19:19:57 -0700 (PDT), "nik@cabana"
<nikhil...@gmail.com> wrote:

<snip>
It's not about how much you can import from excel, it's about how much
data you can have in local arrays on a given system. Local arrays are
allocated on the stack, and maximum stack size can vary between
systems.

You don't need to know what all that means right now. What you do need
to know is that in the program AREA, the array "output" is local, and
in the function xcsv, the array "xx" is local. The compiler might
create another array to store the function value.

All of these arrays are big.

There are a few things you can do:

1. You can change xcsv from a function returning an array to a
subroutine that takes "output" as an argument and fills it with values
read from testdata.csv. This will cut your memory requirement by a
factor of 2, or possibly even 3.

2. You can take a look at your code and see if you really need to read
the whole array in at once. If you're only processing one row at a
time, you only need to read one row at a time. "output" doesn't even
need to be a two-dimensional array. That will cut your memory
requirement by a factor of nrows, or -- in your latest example --
81,000.

3. You can make "output" allocateable. It will live on the heap
instead of on the stack, and you'll be much less likely to run out of
room. You don't need to know what all of this means -- yet.

4. You could figure out how to increase the stack size on your system.

Have fun.

Louis

robin....@gmail.com

unread,
Apr 28, 2017, 6:55:49 AM4/28/17
to
On Friday, April 28, 2017 at 5:30:34 AM UTC+10, nik@cabana wrote:
> Hello all,
>
> I am writing Fortran 77 code to extract data from .csv file into an array. Following is the code:

This is not FORTRAN 77 code. It is Fortran 90 and later.

Ron Shepard

unread,
Apr 28, 2017, 11:50:30 AM4/28/17
to
On 4/28/17 5:55 AM, robin....@gmail.com wrote:
> On Friday, April 28, 2017 at 5:30:34 AM UTC+10, nik@cabana wrote:
>> Hello all,
>>
>> I am writing Fortran 77 code to extract data from .csv file into an array. Following is the code:
>
> This is not FORTRAN 77 code. It is Fortran 90 and later.

Every single line of code in this example has lower case, which was
standardized in f90. Also, it is free form source code which was
introduced in f90. f77 would have required fixed form source code.

>> program xcsv
>> ! read real numbers from CSV file

! comments f90. f77 comments required a "C" in column 1.

>> integer, parameter :: iu=20, nrows = 5, ncols = 32

f77 had parameters, but with a different syntax. This is f90.

>> real :: xx(nrows,ncols)
>> integer :: i, a, b

Both these lines with the "::" are f90 syntax.

>> open (unit=iu,file="a.csv",action="read",status="replace")

I'm not sure when "action" and "replace" were added, but they were not
in f77. f2003 I think, or maybe f2008? The "replace" is actually an
error here.

>> do i=1,nrows

f77 would need a statement label here. This statement is f90.

>> read (iu,*) xx(i,:)

Array syntax was introduced in f90. In f77 this would have required an
implied do loop to read a row of an array.

>> end do

f77 did not have enddo. f77 would have needed a labeled continue statement.

>> write (*,*) 'The stored value is:'
>> write (*,*) xx(1,1)
>> write (*,*) 'Enter the values for a & b:'
>> read (*,*) a,b

Except for the free form and lower case, these four statements would be f77.

>> end program xcsv

end program, end function, and end subroutine statements were introduced
in f90.

$.02 -Ron Shepard
0 new messages