Mohammad Amin
Mohammad Amin

Reputation: 96

Why my code doesn't get compiled in fortran?

This is my code and it doesn't get compiled the output is just this:

1>Error: The operation could not be completed 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I'm using Microsoft visual studio 2019 and intel parallel studio 2020 for fortran.

program Truss
 implicit none
 integer :: Nnodes
 open(unit=1,file='data.txt')
 open(unit=2,file='output.txt')
 read(1,2) Nnodes
 write(2,3) Nnodes
 2  format(7x)
 3  format("Number of nodes:",1I)


end program Truss

The file data.txt contains these:

npoint 3
0 0
2 0
1 1

I wanna read the '3' after npoint that's why I ignore 7 characters.

Upvotes: 0

Views: 154

Answers (3)

Mohammad Amin
Mohammad Amin

Reputation: 96

Thanks for your help. Now according to your suggestions I changed the code this way:

program Truss
implicit none
integer :: Nnodes
open(unit=11,file='data.txt')
open(unit=22,file='output.txt')
read(11,20) Nnodes
write(22,30) Nnodes
20  format(7x,I3)
30  format("Number of nodes:",2I)


end program Truss

Now I want to have 2 lines to be ignored and write the data in output.txt according to format 30, but this is how it does:

Number of nodes:           3

Upvotes: 0

chw21
chw21

Reputation: 8140

When you want to know why an error occurs, it's usually a good idea to include the error message. If it's not printed on the screen, then it should be in some logs.

That said, here are three things I notice, and two of them have been noticed by @evets and @Oo.oO as well:

  • Do not use unit numbers lower than 10. Some of these might be reserved for standard input and output, or error output, or stuff like that.

  • You try to read an integer, but the format that you give does not contain any integer descriptor. 7X just means "ignore 7 characters", but ignoring doesn't read values. Now I don't know what your input file looks like, or why you feel the need to ignore the first 7 characters. Generally it's best to just use

      read(unit, *) Nnodes
    

    but if you do need to declare the format, then that format specifier must contain some component for the actual integer number, like this:

      2 FORMAT(7X, I4)  
    

    This assumes that the 8th through 11th character in the input line contain nothing but the number, and all of it. The 4 after I denotes how many characters the number to be read contains.

  • Finally, there's the format for the print statement. You have 1I -- Numbers before the I indicate how many integers to read. 1 in that case is superfluous. But I am reasonably certain that I needs a number after the I to denote how many digits should be used for the integer.

    Now some compilers seem to accept I0 meaning 'just as many as you need', but I don't know which standard that is and whether your compiler accepts it. Some compilers might also accept just the I but I don't think that's standard conform. (I'm sure someone will correct me there in the comments below this answer ;) )

Cheers

Upvotes: 1

Oo.oO
Oo.oO

Reputation: 13415

It's just a pure guess, but it looks like 7x should be followed by the type specification (e.g. I1) in your case.

program Truss

  implicit none
  integer :: Nnodes

  open (unit=10, file='data.txt')
  open (unit=20, file='output.txt')

  read (10, 2) Nnodes
  write(20, 3) Nnodes

2 format(7X,I1)
3 format("Number of nodes: ", I1)

end program Truss

But, as I said, it's just pure guessing what you are trying to achieve.

I assume that your input file looks like this:

> cat data.txt
       1

Upvotes: 1

Related Questions