Help with Fortran90: Reading a File

  • Fortran
  • Thread starter ne_237
  • Start date
  • Tags
    Fortran90
In summary, the conversation discusses a problem with reading and writing files in Fortran90, specifically with organizing numbers and characters into two columns. Suggestions for using Python instead are also mentioned. The discussion also covers issues with writing arrays and outputting them as a matrix, with possible solutions provided. The conversation ends with a thank you for the help received.
  • #1
ne_237
9
0
Hi All
I have a problem in the work with Fortran90. I have a file that don't have any idea for read it. It consiste of number and character.For example it includes as below
1 15193r 8 9r 4 6 74r,...
I want to write these numbers in 2 columns. For example
1 15193
8 9
4 0
6 74
terms with r show the number of iteratin and number ( that after it dosen't exist term with r) shows that this number has no iteration. Please help me.

Thanks a lot.
 
Technology news on Phys.org
  • #2
Honestly Fortran is one of the worst languages for that type of problem. It would probably be quicker to do it in Python, even if you had to teach yourself the basics of Python first.

Do you have any other languages available?
 
  • #3
Thanks Uart.But I had to solve this problem with Fortran90.
 
  • #4
I have a problem in write arrays also.How I do write rows and columns in file as matrix.
For example I use this program:
do i=1,10
write(12,*)(x(i,j),j=1,20)
end do
but in file only 4columns are showed and 20columns are breaked.Can anyone help me?
Thanks a lot.
 
Last edited:
  • #5
Hi ne 237

If the matrix only has 20 columns then try using to following

write(12,*) x(i,:)

If it's got more than 20 (but you only want to output the first 20) try

write(12,*) x(i,1:20)
 
  • #6
Thanks a lot Uart for reply
But I want to the output will be as below:
1 2 2 3 2 3 4 3 5 3 5 3 6 4 5 8 9 45 65 14
...
...
...
that is 20columns (or more in other program)write in different rows.
 
  • #7
Yeah that's how it works for me (you still need the outer for loop of course).

do i=1,10
write(12,*) x(i,:)
end do

I'm using the freeware gnu fortran compiler "g95" and the above code correctly writes the matrix, whole row per line, to an output text file.
 
  • #8
Thank you very much.
 

1. How do I read a file in Fortran90?

To read a file in Fortran90, you can use the "READ" statement followed by the "OPEN" statement to specify the file name and access mode.

2. What is the syntax for reading a file in Fortran90?

The syntax for reading a file in Fortran90 is:

READ(unit, format) variable1, variable2, ...

OPEN(unit, file=filename, status=status, iostat=iostat)

3. Can I read multiple lines from a file in Fortran90?

Yes, you can read multiple lines from a file in Fortran90 by using a loop and specifying the number of lines you want to read using the "END=" option in the "READ" statement.

4. What are the different access modes for reading a file in Fortran90?

The different access modes for reading a file in Fortran90 are "SEQUENTIAL", "DIRECT", and "STREAM". Sequential access reads the file from beginning to end, direct access allows for random access to specific parts of the file, and stream access reads the file as a continuous stream of data.

5. How do I handle errors while reading a file in Fortran90?

You can handle errors while reading a file in Fortran90 by using the "IOSTAT" and "ERR" options in the "READ" statement. The "IOSTAT" option returns a status code that indicates if the operation was successful, while the "ERR" option allows you to specify a label to jump to in case of an error.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
5
Views
8K
  • Programming and Computer Science
2
Replies
57
Views
3K
  • Programming and Computer Science
Replies
27
Views
2K
  • Programming and Computer Science
Replies
4
Views
712
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top