# Storing data in fortran

• Fortran
yancee
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.

Mentor
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.

yancee
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

gsal
So, what have you tried?

yancee
I've tried this one

Program database
Character (len=30):: name
Print*, 'Input 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.

gsal
In your OPEN statement, you have " action='write' "...have you tried " action='readwrite' " ?

yancee
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
Write(13,*) Allysa
Close (unit=13)

The output is just
Anna
Abby
Allan Allysa

What I want is to write Allysa after Allan.

gsal
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.

gsal
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

do
if (reason < 0) exit
end do
[b][COLOR="Red"]write(13,*)[/COLOR][/b]

print*, 'Input 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

do
if (reason < 0) exit
end do

print*, 'Input name'
write(13,'(a)') name

close (unit=13)

End program

yancee
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: