# Storing data in fortran

• Fortran

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

Related Programming and Computer Science News on Phys.org
Mark44
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.

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

So, what have you tried?

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.

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

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.

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.

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

Thank you. I am going try this because to try this later.