Writing to the .txt file in specific format - fortran

This is my code:
Program NN_Lenght
Implicit none
Integer::i,j
Integer,parameter::Br_Dio=14,Br_NN_Mre=10
Integer,parameter,dimension(Br_Dio,Br_NN_Mre)::Lenght=50
Character(350),parameter::fmtA_0_0='(/,1x,130("-"),/,1x,"LENGHT VALUES NN DM:",/,1x,130("-"))'
Character(350),parameter::fmtA_0_1='(1x,"Network:",1x,10(1x,"||NN_DN",1x,i0,"||"))'
Character(350),parameter::fmtA_0_2='(1x,130("-"))'
Character(350),parameter::fmtA_0_3='(1x,"Dio:",1x,i2,".",2x,9("||",7x,"||",1x),"||",8x,"||",1x,/,1x,130("-"))'
Open(Unit=15,File='Lenght_values_NN.txt',Status='Unknown')
Write(15,trim(adjustl(fmtA_0_0)))
Write(15,trim(adjustl(fmtA_0_1)))(j,j=1,Br_NN_Mre)
Write(15,trim(adjustl(fmtA_0_2)))
Do concurrent(i=1:Br_Dio)
Write(15,trim(adjustl(fmtA_0_3)))i
End Do
Close(Unit=15,Status='Keep')
End Program NN_Lenght
The result of code is Lenght_values_NN.txt file and in that file there is a table_matrix with empty fields(14x10). I want to enter in every empty field of table_matrix the value of Length. That value is 50, just like in variable definition part of the code. How to do that?

Related

Reading the same line from a file many times in Fortran

I would like to read the same line of a file many time in Fortran. The concerned data are real values. I tried to build this code as test but I am always getting it wrong.
program advance
implicit none
integer , parameter :: ut = 20
character(len=7) :: fname = 'dat.dat'
integer :: n, idx
character(len=100) :: lnumber
open(unit = ut, file =fname, status='old', action='read')
n = 10
do idx = 1, n
read(ut, '(a)', advance = 'no') lnumber
print *, lnumber
end do
end program advance
The dat.dat file contains one line with 25.325654515464564564
The code return the following error.
At line 13 of file advance.f90 (unit = 20, file = 'dat.dat')
Fortran runtime error: End of record
How do I fix this bug?
Such non-advancing input (using advance='no') doesn't mean that the file position is not advanced at all. It means that the file position isn't advanced beyond what is needed to satisfy the requirements of the input list.
So, in this case, the file position is advanced by reading the single "real number" into the character variable lnumber. The next read will continue from this later point. This later point happens to be the end of the file.
With advancing input more generally, the file position is advanced to the start of the next record even if the record is not required in entirety.
As High Performance Mark comments, reading the same line over and over again likely isn't what you should be doing. You could read the line into a character variable (such as is done here) and repeatedly use that variable as an internal file. However, if you really want to read a line again, consider backspace.

How to read the last array line in text file in Fortran 77 and save it in an array?

For example, I have a write command to save an array with 6 fields in a text file like this:
OPEN(UNIT=26,FILE='W:\Partikeltemperaturfeld.txt',
&FORM ='FORMATTED',STATUS='UNKNOWN',
&ACTION='READWRITE')
C
WRITE (26,FMT='(6(F8.3,3X))') TFIELD(1,1:6)
C
REWIND(26)
Now I want to read and save the 6 values of the last line of my text file in another array with 6 fields like:
IOS = 0
DO WHILE (IOS.EQ.0)
READ(UNIT=26,FMT='(6(F8.3,3X))',IOSTAT=IOS) TEST(1:6)
END DO
This could be the content of the last line in my file:
1377.445 1373.171 1363.639 1352.062 1341.476 1334.764
The aim is to save the last line in existing format.
But after doing this I always get values 0.000000E+00. How can I read and save the last line of my 'W:\Partikeltemperaturfeld.txt' file in array TEST(1:6)?
If you do
DO WHILE (IOS.EQ.0)
READ(UNIT=26,FMT='(6(F8.3,3X))',IOSTAT=IOS) TEST(1:6)
END DO
the loop will exit when there was some problem (end of file or an error condition) reading the array in the input list. The value of the variable being read is than undefined. You cannot use it. It can contain anything.
BTW your code is not Fortran 77 conforming (it is Fortran 90 or later) so I will not try to make my suggestion 100% Fortran 77 either. You can do
DO
READ(UNIT=26,FMT='(6(F8.3,3X))',IOSTAT=IOS) TMP
IF (IOS.EQ.0) THEN
TEST = TMP
ELSE
EXIT
END DO
END DO
where TEST and TMP are arrays of size 6

Program compiling but not printing results

I have been trying to write a program in Fortran77 and am not able to finish it up, so I need a bit help to make it work.
At first I am just trying to open a .txt file read the file and write the file.
the .txt file is of the format shown below.
001,0.02014,3.1217
002,0.09611,3.1203
003,0.23753,3.1128
004,0.45527,3.0884
005,0.75772,3.0285
where the first column is integer and the second column coma separated contains a real number with 5 digits after the decimal point and third column again a real number with four digits after the decimal point.
My program looks as follows:
PROGRAM FIRST
IMPLICIT NONE
REAL,DIMENSION(304,3)::A
OPEN(UNIT =7 , FILE = "Pressure_values.txt",
1 FORM = "FORMATTED", STATUS = "OLD", ACTION = "READ")
READ(*,100) ((A(I,J),J=1,3), I=1,304)
WRITE(*,100) ((A(I,J),J=1,3), I=1,304)
100 FORMAT(I3,F10.5,F10.4)
STOP
END
Where am I going wrong?
You are not reading from the file. You need to read from unit 7.
read(7,100)
your code:
READ(*,100) ((A(I,J),J=1,3), I=1,304)
means "read from STDIN with format No.100", not the file you just opened. STDIN normally means the keyboard input stream. use:
READ(7,100) ((A(I,J),J=1,3), I=1,304)
instead.

how to write simultaneously in a file while the program is still running

In simple words I have a file which contains duplicate numbers. I want to write unique numbers from the 1st file into a 2nd file. I have opened the 1st file in 'r' mode and the 2nd file in 'a+' mode. But it looks like that nothing is appended in the 2nd file while the program is running which gives wrong output. Any one can help me how do I fix this problem.
Thank you in advance.
This is my code
#!/usr/bin/env python
fp1 = open('tweet_mention_id.txt','r')
for ids in fp1:
ids = ids.rstrip()
ids = int(ids)
print 'ids= ',ids
print ids + 1
fp2 = open('unique_mention_ids.txt','a+')
for user in fp2:
user = user.rstrip()
user = int(user)
print user + 1
print 'user= ',user
if ids != user:
print 'is unique',ids
fp2.write(str(ids) + '\n')
break
else:
print 'is already present',ids
fp2.close()
fp1.close()
If unique_mention_ids.txt is initially empty, then you will never enter your inner loop, and nothing will get written. You should use the inner loop to determine whether or not the id needs to be added, but then do the addition (if warranted) outside the inner loop.
Similar logic applies for a non-empty file, but for a different reason: when you open it for appending, the file pointer is at the end of the file, and trying to read behaves as if the file were empty. You can start at the beginning of the file by issuing a fp2.seek(0) statement before the inner loop.
Either way: as written, you will write a given id from the first file for every entry in the second that it doesn't match, as opposed to it not matching any (which, given the file name, sounds like what you want). Worse, in the second case above, you will be over writing whatever came after the id that didn't match.

How to open and read multiple files in Fortran 90

I have some issues about opening and reading multiple files. I have to write a code which reads two columns in n files formatted in the same way (they are different only for the values...). Before this, I open another input file and an output file in which I will write my results. I read other questions in this forum (such as this one) and tried to do the same thing, but I receive these errors:
read(fileinp,'(I5)') i-49
1
devstan.f90:20.24:
fileLoop : do i = 50,52
2
Error: Variable 'i' at (1) cannot be redefined inside loop beginning at (2)
and
read(fileinp,'(I5)') i-49
1
Error: Invalid character in name at (1)
My files are numbered from 1 to n and are named 'lin*27-n.dat' (where n is the index starts from 1) and the code is:
program deviation
implicit none
character(len=15) :: filein,fileout,fileinp
integer :: row,i,h
real :: usv,usf,tsv,tsf,diff
write(*,'(2x,''Input file .......''/)')
read(*,'(a12)') filein
write(*,'(2x,''Output file........''/)')
read(*,'(a12)') fileout
open(unit = 30,File=filein)
open(unit = 20,File=fileout)
fileLoop : do i = 50,52
fileinp = 'lin*27-'
read(fileinp,'(I5)') i-49
open(unit = i,File=fileinp)
do row = 1,24
read(30,*) h,usv,tsv
read(i,*) h,usf,tsf
diff = usf - usv
write(20,*) diff
enddo
close(i)
enddo fileLoop
end program deviation
How can I solve it? I am not pro in Fortran, so please don't use difficult language, thanks.
The troublesome line is
read(fileinp,'(I5)') i-49
You surely mean to do a write (as in the example linked): this read statement attempts to read from the variable fileinp rather than writing to it.
That said, simply replacing with write is probably not what you need either. This will ignore the previous line
fileinp = 'lin*27-'
merely setting to, in turn, "1", "2", "3" (with leading blanks). Something like (assuming you intend that * to be there)
write(fileinp, '("lin*27-",I1)') i-49
Note also the use of I1 in the format, rather than I5: one may want to avoid blanks in the filename. [This is suitable when there is exactly one digit; look up Iw.m and I0 when generalizing.]