Fortran Program Test: How to Change Line in File

  • Fortran
  • Thread starter kth
  • Start date
  • Tags
    Fortran
In summary, the conversation discusses a program called "test" that opens a file named "test.dat" and writes numbers to it. The output on the screen shows the numbers 111 and 222, but the data file shows them on separate lines with an extra line between them. The question is how to change the program so that the numbers are written on the same line in the data file. The expert suggests checking the compiler and operating system being used.
  • #1
kth
4
0
program test
open(50,file='test.dat')
write(6 ,FMT='(I3,/)') 111
WRITE(6 ,FMT='(I3,/)') 222
write(50,FMT='(I3,/)') 111
WRITE(50,FMT='(I3,/)') 222
close(50)
end

In the above, at the screen i see:
111
222
but in the dat file i see:
111222

How can i change line in the file??
 
Technology news on Phys.org
  • #2
Kth
When I run the above code compiled with F77 fortran, test.dat contains
111

222

with an extra line between the numbers and after the line 222.
On the other hand, the first character on the screen has been used for printer carriage control, namely the first 1 had been taken as a new page.

Are you running with f77 or f90, and on Linux or Windows XP?
 
  • #3


One way to change the line in the file is to use the "backspace" statement in Fortran. This allows you to move the file position back to a previous line and overwrite it with new data. For example, you could use the following code after the first write statement to move the file position back and overwrite the first line with a new value:

backspace(50) ! move file position back one line
write(50,FMT='(I3,/)') 333 ! overwrite first line with new value

This will result in the file containing:

333
222

Alternatively, you can also use the "seek" statement to move the file position to a specific line, and then write new data at that position. For example, you could use the following code to move the file position to the second line and overwrite it with a new value:

seek(50,2) ! move file position to line 2
write(50,FMT='(I3,/)') 333 ! overwrite second line with new value

This will result in the file containing:

111
333

It is important to note that the "backspace" and "seek" statements should only be used when writing to a file that has already been opened and written to. If the file is opened for the first time, it will not have any previous lines to move to or seek to. In that case, you can simply use the write statement as shown in the code provided.
 

1. How do I change a line in a Fortran program file?

To change a line in a Fortran program file, you can use a text editor such as Notepad or an integrated development environment (IDE) like Visual Studio. Simply open the file, locate the line you want to change, make your edits, and save the file.

2. Can I change multiple lines at once in a Fortran program file?

Yes, you can change multiple lines at once in a Fortran program file by using a text editor with a find and replace feature. This will allow you to search for a specific string of characters and replace it with another string, saving you time and effort.

3. What should I do if my changes to a line in a Fortran program file are not taking effect?

If your changes to a line in a Fortran program file are not taking effect, double check that you have saved the file after making your edits. Also, make sure that you are editing the correct file and that your changes are in the correct format for Fortran syntax.

4. Is there a way to undo changes to a line in a Fortran program file?

Yes, most text editors have an undo feature that allows you to reverse any changes you have made to a file. You can also use version control software, such as Git, to track changes and easily revert to a previous version if needed.

5. How do I test my changes to a line in a Fortran program file?

To test your changes to a line in a Fortran program file, you can compile and run the program. This will allow you to see if your changes have had the desired effect. You can also use debugging tools to step through the code and track any errors or unexpected results.

Similar threads

  • Programming and Computer Science
Replies
4
Views
10K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
4
Views
501
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
Back
Top