Storing Data in Fortran: Solving the File Replacement Problem

In summary: DAT file?In summary, you are asking for a fortran code wherein the stored file in DAT file will be stored once the program is run again. You are also asking for a fortran code wherein the stored file in DAT file will be stored once the program is run again, but with the ability to read and write the file.
  • #1
yancee
5
0
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.
 
Technology news on Phys.org
  • #2
yancee said:
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
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
So, what have you tried?
Have you read about the OPEN statement and its arguments?
 
  • #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.
 
  • #6
In your OPEN statement, you have " action='write' "...have you tried " action='readwrite' " ?
 
  • #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.
 
  • #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.
 
  • #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:
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
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
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...
 

What is the purpose of storing data in Fortran?

The purpose of storing data in Fortran is to allow for the efficient manipulation and analysis of large datasets within a scientific or engineering context. Fortran is a high-performance language that is commonly used for numerical and scientific computing, and its data storage capabilities are optimized for this purpose.

What are the different methods for storing data in Fortran?

There are several methods for storing data in Fortran, including arrays, structures, and variables. Arrays are the most commonly used method and allow for the storage of large amounts of data in a single variable. Structures allow for the grouping of related data, and variables are used for storing single values.

How do you declare and initialize data in Fortran?

Data in Fortran is declared using the "parameter" keyword, followed by the name and value of the data. For example, "parameter :: pi = 3.14159" declares a constant variable named "pi" with a value of 3.14159. Data can also be initialized using the "data" statement, which assigns values to variables within the program.

What are some common data types used in Fortran?

Fortran supports a variety of data types, including integers, real numbers, and logical values. Other common data types include character strings, complex numbers, and arrays. Fortran also has specific data types for handling dates and times, as well as for representing units of measurement.

How is data stored and accessed in Fortran?

Data in Fortran is stored in memory, and its location can be accessed using its name and the "%" operator. For example, "array(3)%" would access the third element of the array named "array." Data can also be stored and accessed in files using input/output (I/O) operations, which allow for the reading and writing of data to external sources.

Similar threads

  • Programming and Computer Science
Replies
1
Views
274
  • Programming and Computer Science
Replies
3
Views
383
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
14
Views
628
  • Programming and Computer Science
Replies
11
Views
994
  • Programming and Computer Science
Replies
22
Views
919
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
376
Back
Top