Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Storing data in fortran

  1. Sep 25, 2012 #1
    I was looking for a fortran code wherein the stored file in DAT file will be stored once I run the program again. My program is just replacing the saved file.
     
  2. jcsd
  3. Sep 26, 2012 #2

    Mark44

    Staff: Mentor

    What you wrote is not clear. What are you asking?

    In any case, we're not going to write your code for you. We'll help you, but you need to show some effort.
     
  4. Sep 26, 2012 #3
    I was making a database program in fortran. I was just adding profile such as name of the students and other information about him/her. Then the data that the user inputs will be saved in a DAT file. I want to add another data in the said DAT file wherein the first data that was been input by the user will be stored and I could write other information after the line on the first data
    The DAT file was like this:
    Name: Olave, Yancee
    Student number: 2010-45673
    Then if I add another data, this will be like this:
    Name: Olave, Yancee
    Student number: 2010-45673
    Name: Garcia, Kyle
    Student number: 2011-78461
     
  5. Sep 26, 2012 #4
    So, what have you tried?
    Have you read about the OPEN statement and its arguments?
     
  6. Sep 26, 2012 #5
    Yes, I have read about open statements.
    I've tried this one

    Program database
    Character (len=30):: name
    Print*, 'Input name'
    Read*, name
    Open (unit=13, file='data.dat', status='new', action='write')
    Write(13,*) name
    Close (unit=13)
    End program

    As I run the program again, the program is just creating new DAT file and what I want is to use the DAT file created first and then write another information in that file.
     
  7. Sep 26, 2012 #6
    In your OPEN statement, you have " action='write' "...have you tried " action='readwrite' " ?
     
  8. Sep 26, 2012 #7
    I have tried it now but the data that I input was just written after the last line on my file. For example, in my DAT file, i have written
    Anna
    Abby
    Allan
    and if i try the readwrite action just like this
    Open (unit=13, file='data.dat', status='new',action='readwrite')
    Read(13,*) a,b,c
    Write(13,*) Allysa
    Close (unit=13)

    The output is just
    Anna
    Abby
    Allan Allysa

    What I want is to write Allysa after Allan.
     
  9. Sep 27, 2012 #8
    Please post your entire, actual code; surround it with 'code' and '/code' tags, where the tags themselves are to be enclosed in square brackets. Also post your data file.
     
  10. Sep 27, 2012 #9
    o.k., I played around a bit with this...there are a couple of choices.

    This is how things are working for me.

    1.-
    If the data file ends exactly at the of the last line...and I mean exactly at the right hand end of the line as if you had type the last line and NOT pressed <ENTER>, then, all you have to do is:
    - do a WRITE after reading the file's contents, and
    - write new content with the " advance='no' " option to achieve the same effect (end of file at end of last line written).

    Sample data file
    Code (Text):

    Anna
    Abby
    Ammy      <- end of file is here
     
    Code
    Code (Text):

    Program database
        integer reason
        character (len=30):: name, a,b,c
       
        open (unit=13, file='rw.dat', status='old', action='readwrite')
        do
            read(13,'(a)',iostat=reason) a
            if (reason < 0) exit
        end do
        [b][COLOR="Red"]write(13,*)[/COLOR][/b]
       
        print*, 'Input name'
        read*, name
        write(13,'(a)',[b][COLOR="red"]advance='no'[/COLOR][/b]) name
       
        close (unit=13)
       
    End program
    2.-
    If the data file end on the line following the last line with content, then, things are pretty normal:
    - you don't need to insert a WRITE before writing new contents
    - you don't need to use the " advance='no' " option

    Sample data file
    Code (Text):

    Anna
    Abby
    Ammy
    <- end of file is here
     
    Code
    Code (Text):

    Program database
        integer reason
        character (len=30):: name, a,b,c
       
        open (unit=13, file='rw.dat', status='old', action='readwrite')
        do
            read(13,'(a)',iostat=reason) a
            if (reason < 0) exit
        end do
       
        print*, 'Input name'
        read*, name
        write(13,'(a)') name
       
        close (unit=13)
       
    End program
     
     
  11. Sep 28, 2012 #10
    Thank you. I am going try this because to try this later.
    I also have another question. What if I am going to ask the user to input username and pass code so that she can create a profile, and that username and pass code will be written in a DAT file. After that, I also want to the user to add another profile with another username and pass code and then save to the DAT file of usernames and passcode. This will mean that there will come a time that there were several usernames and pass code. That usernames and pass codes will be use to print the saved profile when she run the program again. I want to know the code wherein i could be able to check the username she input for printing profile if it is match to one of the usernames in the DAT file for usernames and then the same goes on with the pass code. I just tried:
    Open (unit=13, file='usernames.dat', status='old')
    Read(13,*)

    And then the file that has been read was just the first line in the username.dat. This means that I am not able to check the other usernames in the file.
     
  12. Sep 28, 2012 #11
    ahem....it's called programming. Fortran or not, I think your problem might just be programming. You need to not be shy and play around with the program, it is not going to explode or break anything...just think of what you would be ' by hand ' and the try to program it....read a line, compare against another string, if not equal read another line, and so on...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Storing data in fortran
Loading...