Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open and write to a file

Status
Not open for further replies.

solalito

Programmer
Jul 15, 2010
6
US
Hey I'm all new to fortran. I've had problems with a bigger code writing data to files. So now I've written the simplest code of all but still cannot write and open a file! I'm have a mac (w/ snow leopard) and I'm using gfortran to compile my code. The code compiles fine but there is no file to be seen. Also, the PRINT command doesn't do anything. Clearly something's off but I can't figure out what.

PROGRAM test

IMPLICIT NONE

INTEGER :: i

OPEN(UNIT=505,FILE="new.txt")

i = 100

PRINT*,i

WRITE(10,'(I7)') i

CLOSE(10)

END PROGRAM
 
correct the unit id in open statement
Code:
OPEN(UNIT=10,FILE="new.txt")
 
it doesn't work.. Even the print command doesn't do anything
 
I tried it with g95 and gfortran and it works:
I get on the screen 100 and in the file new.txt the same.
What compiler are you using?
Maybe code the status in the open statement:
Code:
OPEN(UNIT=10,FILE="new.txt", status="unknown")
 
I'm using fortran. I got a mac. I've tried to compile it using two methods from X11:
gfortran -o testrun -ffree-form test.f90

gfortran -c test.f90
gfortran -o testrun test.o

 
I'm using gfortran too and it works:
Code:
$ gfortran solalito.f95 -o solalito

$ solalito
         100
 
Let's say my file is named test.f90 what would you enter on the terminal to compile it?
 
Compile:
Code:
$ gfortran test.f90 -o test
Then run the executable:
Code:
$ test
 
I don't see why

Code:
gfortran -c test.f90
gfortran -o testrun test.o

wouldn't work. It should work, and does work, when I compile it like this.

Code:
gfortran test.f90 -o testrun
does it in one step, but it should give the same result.

Solalito, are you sure your gfortran binaries (installation) is OK? Can you try another installation from somewhere else? Where did you download it from?

From here:


I see that the one found here:


is supposed to work well, whereas the HPC ( one doesn't.

I don't have access to a Mac, so I can't confirm this, but you should be able to.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
So I was using gfortran from HPC but since you told me it was rubbish I just downloaded it from Now when entering
gfortran test.f90 -o testrun I get:

ld: warning: in /usr/local/lib/libgfortran.dylib, file was built for i386 which is not the architecture being linked (x86_64)
Undefined symbols:
"__gfortran_st_write", referenced from:
_MAIN__ in cc9zz0ju.o
_MAIN__ in cc9zz0ju.o
"__gfortran_st_close", referenced from:
_MAIN__ in cc9zz0ju.o
"__gfortran_set_std", referenced from:
_MAIN__ in cc9zz0ju.o
"__gfortran_transfer_integer", referenced from:
_MAIN__ in cc9zz0ju.o
_MAIN__ in cc9zz0ju.o
"__gfortran_st_open", referenced from:
_MAIN__ in cc9zz0ju.o
"__gfortran_set_args", referenced from:
_main in libgfortranbegin.a(fmain.o)
"__gfortran_st_write_done", referenced from:
_MAIN__ in cc9zz0ju.o
_MAIN__ in cc9zz0ju.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


I am using Xcode 3.2.3 and it did recommend version 3.2.2 but I'll be surprise if thats the problem. Why did they make that compiler so hard to use...
 
Solalito, it just seems that you downloaded the x86 (32-bit) version, but your system is x64 (64-bit). Furthermore, are you on Intel or PowerPC?

Maybe Google "gfortran binaries mac 64-bit <processor>" where <processor> is either "intel" or "powerpc", depending on what you're running, and see where that gets you.

Regarding g95 vs gfortran, I think it's best to have both. g95 is better for debugging than gfortran (it gives more useful messages, and -ftrace=full is a useful flag to see where runtime errors happen), but does let some things compile which shouldn't according to the standard (such that your code might not be portable to other compilers). Also, I find it about 3-5 times slower than gfortran for intensive calculations -- and let's face it, isn't that why we're using Fortran in the first place?

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
ya I was recommended to use gfortran because the code that I hopefully will be dealing with soon are big numerical simulations (CFD), and I was told fortran was the most efficient language out there.
I have a intel processor. I removed the gfortran I just got from

Do you know where I could find a working gfortran pckg for x64?
Thanks to you too btw for everything
 
Read the discussion on the link above posted by NickFort.
Some people there suggest to install gcc44 which includes gfortran.
 
Solalito, no idea... Google will have to do. But yes, as that post said, search for x64 binaries for GCC.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
They say, you should install first Fink and then gcc.
No idea what Fink is - I thing somethink like MSYS+MinGW or Cygwin but for Mac.
But you can try the g95 compiler too. Although NickFort says that g95 is slower that gfortran I'm using it rather. You can have both compilers, because they are both free. The advantage using both compilers is that you can proove, if your code works with both - if not then it's not good and it must be standardized.
 
Hey solalito, not sure if you have gfortran running yet, but I came across this today and thought it might help you:


--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top