Reading a file in FORTRAN 90 or 95

In summary, the conversation is about a person seeking help with reading a text file using FORTRAN 90/95. They want to put each line of the file into a string variable, but are having trouble with the code they are using. Another person asks for more specific information and the original person replies that they were able to solve the problem with the help of a friend.
  • #1
hamidsv
3
0
Hi everybody,
I need your help guys. I am using FORTRAN 90(or 95) and try to read a text file. The file contains many lines for instance something like this
CS
DIRECT
1.2 2.5 3.6
1.6 -2.3 2.9
....
....
....
I need to put each line inside a string variable for example
A="CS"
B="DIRECT"
C ="1.2 2.5 3.6 "
....
Please if you know the answer just let me know. I use "READ(1,*,IOSTAT=EOF) A" but the result is totally different.
 
Technology news on Phys.org
  • #2
Hi, hamidsv:

When it comes to source code, input and results, hand waving does not cut it; please post the actual source code that you are using, an actual sample of the input and the actual results that you are getting...there is no point in trying to help you when you tell us what YOU think what you are doing but the sources say something else.
 
  • #3
Hi gsal,
Thanks for your quick reply. This time I was able to solve the problem (one of my friends helped me). Next time I try to provide all information if I have any questions.
Thanks again
 

1. How do I read a file in FORTRAN 90 or 95?

To read a file in FORTRAN 90 or 95, you will need to use the OPEN statement to specify the file you want to read from. You will also need to use the READ statement to read the data from the file into your program. Make sure to include the appropriate FORMAT statement to specify how the data is formatted in the file.

2. What is the difference between reading a formatted and unformatted file in FORTRAN 90 or 95?

A formatted file is one where the data is stored in a human-readable format, with spaces and line breaks included. An unformatted file is one where the data is stored in a binary format, without any spaces or line breaks. In FORTRAN 90 or 95, you can use the FORM='FORMATTED' or FORM='UNFORMATTED' option in the OPEN statement to specify how you want to read the file.

3. Can I read multiple files in FORTRAN 90 or 95?

Yes, you can read multiple files in FORTRAN 90 or 95 by using multiple OPEN and READ statements. Make sure to specify unique file names for each file you want to read from.

4. How do I handle errors when reading a file in FORTRAN 90 or 95?

You can handle errors when reading a file in FORTRAN 90 or 95 by using the ERR keyword in the OPEN statement. This will allow you to specify an error handling routine to execute if there are any issues with opening or reading the file.

5. Can I read a file that is located in a different directory?

Yes, you can read a file from a different directory in FORTRAN 90 or 95 by specifying the full path to the file in the OPEN statement. Alternatively, you can use the INQUIRE statement to get the current working directory and then use the CHDIR statement to change to the directory where the file is located before reading it.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
1
Views
347
  • Programming and Computer Science
Replies
1
Views
524
  • Programming and Computer Science
Replies
6
Views
974
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
2
Replies
57
Views
3K
Back
Top