Fortran90 reading in a formatted input file

In summary, the conversation is about a user trying to use a code written by another engineer, asking for help on how the input file should look to feed into the code correctly. Suggestions were made to use write statements to test the input file and to check the status of the open statement. There were also discussions on potential errors and how to debug them, as well as suggestions to modify the open statement for better results.
  • #1
Rowdy5000
5
0
Hi All,

Let me preface by saying this is my first post, but I'm very glad to have found and joined what looks like a great community here. Also I have no Fortran experience, so please bear with my greenness:

I'm trying to resurrect and use a code written by another engineer in our organization some years ago. I have the following code for opening and reading an input file, now my question is how the input file should look to feed into this correctly.

print*,'Opening / Reading Input File: ...'
print*
open(10,file='input',status='unknown',form='formatted')
read(10,*)a
read(10,*)b
read(10,*)c,d
read(10,*)e,f,g,h
read(10,*)i
read(10,*)j
read(10,*)ntimes
do i=1,ntimes
read(10,*)time(i)
enddo
close(10)

I'm sorry if this is very basic; I'm very new at this!

Thanks!
 
Technology news on Phys.org
  • #2
Rowdy5000 said:
Hi All,

Let me preface by saying this is my first post, but I'm very glad to have found and joined what looks like a great community here. Also I have no Fortran experience, so please bear with my greenness:

I'm trying to resurrect and use a code written by another engineer in our organization some years ago. I have the following code for opening and reading an input file, now my question is how the input file should look to feed into this correctly.

print*,'Opening / Reading Input File: ...'
print*
open(10,file='input',status='unknown',form='formatted')
read(10,*)a
read(10,*)b
read(10,*)c,d
read(10,*)e,f,g,h
read(10,*)i
read(10,*)j
read(10,*)ntimes
do i=1,ntimes
read(10,*)time(i)
enddo
close(10)

I'm sorry if this is very basic; I'm very new at this!

Thanks!

What you can try is switching your read statements leading up to your DO/ENDDO loop with write statements and seeing what the file looks like. It would look like

Code:
open(10,file='test.txt')
      write(10,*)a
      write(10,*)b
      write(10,*)c,d
      write(10,*)e,f,g,h
      write(10,*)i
      write(10,*)j
      write(10,*)ntimes
      close(10)

With arbitrary values assigned to a-j and ntimes.

Just an idea.
 
  • #3
Thanks for that; it did show me how basic of an input I could use.

I am having some trouble with this in debugging though. I get a ".exe triggered a breakpoint" error at the line of the first read statement. I'll do some more headscratching and then return with more details if I can't get it.

Thanks again.
 
  • #4
"Triggering a breakpoint" isn't an error. It seems that you set a breakpoint at that statement, which causes the debugger to stop.
 
  • #5
Mark44 said:
"Triggering a breakpoint" isn't an error.
It could be an error, if the compiler generates breakpoint instructions (INT 3) or uses an invalid address to access memory in a failure case.

It's possible that the open statement failed. Is there anyway to check the status of the open statement?
 
  • #6
rcgldr:

I think you might be close to home w/ suspecting the open statement. In Visual Studio 2005 I get a little green arrow pointing at the first read statement. When I hover over that for detail, I get the following message:

"This code has called into another function. When that function is finished, this is the next statement that will be executed."

And when I go to disassembly, I've got a little yellow arrow pointing at the line just after a line that reads:

"00431340 int 3"

I don't really know yet how to check the status of the open statement.

Thanks to all for the support.
 
  • #7
Rowdy5000 said:
rcgldr:

I think you might be close to home w/ suspecting the open statement. In Visual Studio 2005 I get a little green arrow pointing at the first read statement. When I hover over that for detail, I get the following message:

"This code has called into another function. When that function is finished, this is the next statement that will be executed."

And when I go to disassembly, I've got a little yellow arrow pointing at the line just after a line that reads:

"00431340 int 3"
This is the breakpoint instruction that rcgldr mentioned.

Have you set a breakpoint? If so, it will show up as a red circle in the left margin in Visual Studio. You can delete all breakpoints by clicking Delete All Breakpoints in the the Debug menu in VS. This menu item doesn't appear if there are no breakpoints set.
Rowdy5000 said:
I don't really know yet how to check the status of the open statement.

Thanks to all for the support.
 
  • #8
I think the compiler is automatically setting a breakpoint or something...I got a disassembly display and a call stack that mean nothing to me and I think I've hit a wall with self-diagnosis. If anyone can suggest a new lead or what I could post here to clue us in, I'll gladly oblige.
 
  • #9
AFAIK, the compiler doesn't automatically set breakpoints.

Whatever, your open statement doesn't look right to me, especially that form = "..." part. Here's a link to a page that has several examples of open statements. Try modifying your open statement and see if that makes a difference.

http://www.livephysics.com/computational-physics/fortran/fortran-file-handling.html

For additional examples, you can do what I did, which was enter "fortran open" in the browser.
 
  • #10
Mark44 said:
AFAIK, the compiler doesn't automatically set breakpoints.

Whatever, your open statement doesn't look right to me, especially that form = "..." part. Here's a link to a page that has several examples of open statements. Try modifying your open statement and see if that makes a difference.

http://www.livephysics.com/computational-physics/fortran/fortran-file-handling.html

For additional examples, you can do what I did, which was enter "fortran open" in the browser.

In addition to the link Mark44 provided, I have also found http://www.math.hawaii.edu/~hile/fortran/fortmain.htm" [Broken] to be infinitely helpful.
 
Last edited by a moderator:
  • #11
Here's another link to a page with more than you want to know about the OPEN statement - http://www.hpc.unimelb.edu.au/doc/f90lrm/lrm0545.htm [Broken].

This page describes the FORM parameter, which I didn't remember using (it's been a long while since I did any Fortran programming...).

I don't know if you noticed it, but there's an extra space in the code you showed (in 'format ted'). If that's actually in your code, it's probably what's causing the compiler to complain.
Rowdy5000 said:
Code:
open(10,file='input',status='unknown',form='format ted')
 
Last edited by a moderator:
  • #12
Thanks to everyone for the useful links and support with this. I actually resolved this finally by toggling status in the open statement to 'old'. It didn't really make sense to me based upon what I'd read about the status character, but I tried this and it worked.
 

1. How do I read in a formatted input file in Fortran90?

In Fortran90, you can use the READ statement to read in a formatted input file. This statement requires a format specifier, which defines the structure of the input data. You can also use the OPEN statement to open the file for reading.

2. What is a format specifier in Fortran90?

A format specifier in Fortran90 is a string of characters that defines the structure of the data being read in. It specifies the number of characters to be read, the type of data (integer, real, character), and any special formatting instructions (decimal places, field width, etc.).

3. Can I read in multiple lines of data from a formatted input file in Fortran90?

Yes, you can use a loop to read in multiple lines of data from a formatted input file in Fortran90. You can use the DO loop to read in a specific number of lines or use the END specifier to read until the end of the file is reached.

4. How do I handle errors while reading in a formatted input file in Fortran90?

You can use the ERR specifier with the READ statement to handle errors while reading in a formatted input file in Fortran90. This allows you to specify what should happen if an error occurs, such as skipping the line or stopping the program.

5. Can I read in data from a non-standard formatted input file in Fortran90?

Yes, you can use the ADVANCE specifier with the READ statement to read in data from a non-standard formatted input file in Fortran90. This allows you to skip over certain characters or lines in the input file to properly read in the data.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
374
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
808
Back
Top