Reading in sections of files in Fortran

Discussion in 'Mac Programming' started by Spanky Deluxe, Apr 23, 2008.

  1. Spanky Deluxe macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #1
    Is there any way to read in large chunks of a file piece by piece?

    I.e. say a file consists of 16 numbers. I'd first want to read in the first 4, do some stuff to them, then load in the second 4 etc.

    I'm using the READ command at the moment but I don't know how to do this. The problem isn't reading in the first batch but getting it to read the second batch etc starting at the respective value, i.e. the 5th value in this over-simplified example.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    the open function takes a recl parameter that can be set to the number of bytes per record. Then when you do a read you give a record number.
    Code:
      integer(4) :: z(4)
      integer(2) :: io_status
      open(unit=10,file="./testfile.in",action='read', &
       & status='old',recl=16,access='direct',iostat=io_status)
      if(io_status /= 0) call abort()
      read(unit=10,rec=1,iostat=io_status) z(1:4)
      if(io_status /= 0) call abort()
    -Lee
     
  3. Spanky Deluxe thread starter macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #3
    Awesome! Thanks for that! Big help. :)
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    No problem. I'm glad working with Fortran the last 4 years came in handy SOMEWHERE else.

    -Lee
     
  5. ign macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #5
    read from a text file in fortran

    Hello,
    I am also trying to read from a file in fortran but so far unsuccessfully.

    my file looks like this [stationID and associated characteristic]:

    A2332110 PLUVI
    A2352020 PLUVI
    A3301010 PLUVI
    A3422010 NIVAL
    A3792010 PLUVI
    A3832010 PLUVI
    A4050620 TRANS

    how can I make fortran read it? thank you in advance, Ig

    I have tried with this but it gives me error (i am new to fortran...)
    program readstregime

    CHARACTER(LEN=8) :: STATION
    CHARACTER(LEN=5) :: REGIME

    OPEN (UNIT=1, FILE="STATIONVSREGIME.TXT", STATUS='OLD')
    READ(1,*) STATION, REGIME
    PRINT*, STATION, REGIME


    END​
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Give this a try:
    Code:
    program readstregime
      character(8) :: station
      character(5) :: regime
      integer :: stat
      logical :: reading = .true.
    
      open (unit=1, file="STATIONVSREGIME.TXT", status='OLD',iostat=stat)
      if(stat /= 0) then
        write(7,'("Could not open file, exiting!")')
        stop
      end if
      do while(reading) 
        read(1,'(a8,1x,a5)',iostat=stat,end=1) station, regime
        if(stat /= 0) then
          write(7,'("Could not read from file, exiting!")')
          stop
        endif
        write(6,'("Station: ",a," Regime: ",a)') station, regime
        cycle
    1   reading=.false.
      end do
      close(1)
    end program readstregime
    
    It's well past 1977, so all caps are not necessary. I don't like to feel like my code is shouting at me =).

    -Lee
     
  7. ign macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #7
    I got a
    Could not open file, exiting!
    But thanks a lot for helping me
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Is STATIONVSREGIME.TXT in the directory you're running the program from? Are the permissions such that the user you're running as can read it?

    -Lee
     
  9. ign macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #9
    yes the file is in the same directory in which i have run other fortran programs too so I have the permissions.

    I attach the file STATIONVSREGIME.TXT maybe i'm doing something wrong ...

    Ig
     

    Attached Files:

  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    Pull out the iostat= argument to open, and see what error the runtime gives you. The code worked as posted on my machine with a copy and paste of the data in your post.

    -Lee
     
  11. ign macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #11
    here's what I got after pulling out the iostat argument:

    At line 7 of file readstregime.f90
    Fortran runtime error: No such file or directory​

    line 7 --> open (unit=1, file="stationvsregime.TXT", status='OLD')

    what could that be?
     
  12. ign macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #12
    It works now!

    Here's what I did: saved the content of the text file (STATIONVSREGIME.TXT)
    as text UTF-8 without the .txt extension

    this way I don't get errors and it works. Maybe it depends on the compilers I don't know
    I am using gfortran on Ubuntu Linux.

    Thanks a lot Lee

    By the way I was trying to install gfortran on my home machine (mbp) but ran into some errors. I had even installed the apple development tools... What is the best way to compile fortran under OSX?
     
  13. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    I use g95 most frequently... there was some discussion about getting a fortran compiler set up in this thread:
    http://forums.macrumors.com/showthread.php?t=660181

    As for the filename... string literals (anything enclosed in apostrophes or quotes) are pretty much the only thing where case will matter, so just make sure if a filename is all caps, that's how it appears in code, etc.

    -Lee
     
  14. ign macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #14
    how to write output file to specific directory ~fortran

    Hello,
    I am running a fortran f90 program which writes several output files,

    I would like these files to be saved to a specific folder (not to the
    current default directory)

    here's a write sentence:

    open(unit=3, file=val(i,1)//'_Brutes_lowV.txt', status='replace')
    do h=1, size(pmv)
    write(3,*) YearList(h), pmv(h), lowV(h,:)
    enddo
    close(3)
    where do I add instructions so that the file is written say in
    an output folder located on the desktop (/home/ign/desktop/output)?

    I have tried this (adding the path before the file name) but it hasn't worked!

    open(unit=3, file='home/ign/desktop/output/val(i,1)//'_Brutes_lowV.txt'', status='replace')

    thank you in advance,
    ign
     
  15. ign macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #15
    solved the problem!!

    and to make it slim I declared a path

    CHARACTER*29 :: path='/home/....../output/'

    which I then added to the sentence OPEN(....

    open(unit=3, file=path//val(i,1)//'_Brutes_lowD.txt', status='unknown')​
     

Share This Page