Storing data in fortran

  • Fortran
  • Thread starter yancee
  • Start date
  • #1
5
0

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
33,104
4,800
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.
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.
 
  • #3
5
0
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
 
  • #4
1,065
53
So, what have you tried?
Have you read about the OPEN statement and its arguments?
 
  • #5
5
0
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.
 
  • #6
1,065
53
In your OPEN statement, you have " action='write' "...have you tried " action='readwrite' " ?
 
  • #7
5
0
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.
 
  • #8
1,065
53
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.
 
  • #9
1,065
53
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:
Anna
Abby
Ammy      <- end of file is here
Code
Code:
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:
Anna
Abby
Ammy
<- end of file is here
Code
Code:
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
 
  • #10
5
0
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.
 
  • #11
1,065
53
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...
 

Related Threads for: Storing data in fortran

Replies
2
Views
1K
Replies
1
Views
3K
Replies
8
Views
3K
  • Last Post
Replies
4
Views
2K
Replies
6
Views
1K
  • Last Post
Replies
5
Views
2K
Replies
1
Views
2K
Replies
7
Views
1K
Top