Read large file in Fortran - fortran

The following code succeeded in reading txt files of 30x30 (7Kb) and 100x100 (69Kb) matrices of 1 and 0 values. Here, there is an example of a 3x3 matrix. The txt file "MyTxtFile.txt" reads as follows.
1 0 1
1 0 0
1 1 1
The code is presented bellow.
Integer,dimension(3,3):: ReadMatrix
Integer::row,col
open(12, file="MyTxtFile.txt")
do row=1,3
read(12,*) (ReadMatrix(row,col),col=1,3)
end do
print*, ReadMatrix (1,:) !Print first row to check
As an output, I obtain the following
1 0 1
However, when I tried with a 1000x1000 matrix (3186 Kb) an error appeared saying the following:
At line 8 of file C:\Users\...\...\... (unit=12, file='Matrix1000.txt')
Fortran runtime error: End of file
What is actually happening and what are my options?

Related

Reading records from a file in FORTRAN66 using stdin adding extra unwanted junk

I'm trying to read a file in the format specified below using FORTRAN 66.
1000
MS 1 - Join Grps Group Project 5 5
Four Programs Programming 15 9
Quiz 1 Quizzes 10 7
FORTRAN Programming 25 18
Quiz 2 Quizzes 10 9
HW 1 - Looplang Homework 20 15
I execute and read the file like so:
program < grades.txt
The first line is the total number of points that can be earned in a class
The rest of the lines are assignments in a class
Each line is formatted as such: Assignment name(20 chars) category (20 chars) possible points(14 chars) earned points(14 chars)
For some reason, when the code runs and reads the file, starting at the first assignment record, I get error 5006, and cannot find an explanation of the error code. The output of the program while debugging looks like this:
$ file < grades.txt
MS 1 - Join Grps Group Project 5 6417876
NOT EOF
EOF 5006
NAME CATEGORY POSSIBLE EARNED
My goal is to be able to read each line and put each column into it's appropriate array, then reference those arrays later on to print a report for each category, with each assignment, points possible, earned, and total percentage for the category, then loop, etc.
I do not understand where the "6417876" in the output is coming from, it is definitely not part of the file that's being piped into stdin while the program reads.
The code for the program is as follows:
CHARACTER*20 ASSIGNMENTT(100)
CHARACTER*20 CATEGORY(100)
INTEGER POSSIBLE(100)
INTEGER EARNED(100)
INTEGER TOTALPTS
INTEGER REASON
INTEGER I, N
READ(5,50)TOTALPTS
50 FORMAT(I4)
c Read the arrays in
I=1
100 READ(5,110,IOSTAT=REASON)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
110 FORMAT(2A20x,2I14x)
WRITE(*,110)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
I=I+1
IF (REASON < 0) GOTO 120
WRITE(*,*)"NOT EOF"
IF (I<100 .AND. REASON == 0) GOTO 100
WRITE(*,*)"EOF", REASON
c Get the number of items (For some reason stdin adds an extra item that's not in the file, so I subtract 2 instead of 1
120 N=I-2
c Display the Names and Ages
WRITE(*,200)
200 FORMAT("NAME",T20,"CATEGORY",T40,"POSSIBLE",T54,"EARNED",T68)
DO 300 I=1,N
210 FORMAT(A20,A20,I14,I14)
300 WRITE(*,210)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
END
What could be causing the read issues I'm facing?
The line to read the file contents was too long, so I shortened the names of the variables to save some space and the problem was solved.

Writing a Matrix Array of 2 Rows by 3 Columns, To an Output Text File in Fortran 95

I'm currently learning how to write Matrix Arrays to output Text Files in Fortran 95. The problem I'm facing is that, the Matrix Array of 2 Rows by 3 columns I'm working on, is not formatting to what I desire in the Output Text File. I believe, I'm missing One or Two Lines of Codes or failing to add a few codes to the current Lines of Codes I have. Below are My Lines of Codes, Current Output Data and Desired Output Data. The Goal is to get the "Desired Output Data". Kindly show me My mistake(s), what codes/line(s) of codes I'm missing and where I should add the codes/line(s) of codes. Every answer is welcomed and appreciated. Thank you Stackovites.
Lines of Codes:
Program Format2
!illustrates formatting Your Output
Implicit None
Integer:: B(2,3)
Integer:: k,j
!Open Output Text File at Machine 8
Open(8,file="formatoutput2.txt")
Data B/1,3,5,2,4,6/
Do k= 1,2
B(2,3)= k
!Write to File at Machine 8 and show the formatting in Label 11
Write(8,11) B(2,3)
11 format(3i3)
End Do
Do j= 3,6
B(2,3)= j
!Write to File at Machine 8 and show the formatting in Label 12
Write(8,12) B(2,3)
12 format(3i3)
End Do
End Program Format2
Current Output Data
1
2
3
4
5
6
Desired Output Data
1 3 5
2 4 6
B(2,3) refers only to one particular element of the array. Namely the element with index 2 in the first dimension and index 3 in the other dimension. To refer to a different element use B(i,j) where i and j are integers with the desired index. To refer to the whole array use just B or alternatively B(:,:) for an array section that encompasses the whole array.
So to set the values
do j = 1, 3
do i = 1, 2
B(i,j) = i + (j-1) * 2
end do
end do
and to print them use one of the methods showed in countless duplicates (Print a Fortran 2D array as a matrix Write matrix with Fortran How to write the formatted matrix in a lines with fortran77? Output as a matrix in fortran -- search for more, there will be better ones...) on this site
do i = 1, 2
write(8,'(999i3)') B(i,:)
end do
I've seen My mistakes. The instructions I gave the Fortran Compiler, was the result I got in My Output Text File. I was declaring two-2 Rows of (1,2) and (3,4,5,6); instead of declaring Three-3 Columns of (1,2); (3,4) and (5,6). Below is the correct Lines of Codes to get the Desired Output Data.
Lines of Codes:
Program Format2
!illustrates formatting Your Output
Implicit None
Integer:: B(2,3)
Integer:: k,j
!Open Output Text File at Machine 8
Open(8,file="formatoutput2.txt")
Data B/1,2,3,4,5,6/
!(a)Declare The 1st Two-2 Values You want for k Two-2 Rows, that is (1 and 2)
!(b)Note, doing (a), automatically declares the values in Column 1, that is (1 and 2)
Do k= 1,2
B(2,3)= k
End Do
!(c)Next, Declare, the Four Values You want in Column 2 and Column 3. That is [3,4,5,6]
!(d) Based on (c), We want 3 and 4 in "Column 2"; while "Column 3", gets 5 and 6.
Do j= 3,6
B(2,3)= j
End Do
!Write to File at Machine 8 and show the formatting in Label 13
Write(8,13) ((B(k,j),j= 1,3),k= 1,2)
13 format(3i3)
End Program Format2
The Above Lines of Codes, gives the Desired Output below:
1 3 5
2 4 6

Fortran read function does not read the last value

I have a input file (filename: L0analysis) like below
...... !headers
3 2.1 2.4 2.5 !ntfmr rcut(1) rcut(2) rcut(3)
...... !other data
The index i in rcut(i) is determined by ntfmr
in my program,
......
ncrit=55
open(ncrit,file='Lanalysis')
......!(read the headers. skipped here)
read(ncrit,*) ntfmr, (rcut(i),i=1,ntfmr)
do i=1,ntfmr ! # of network formers and their cutoffs
rcut2(i)=rcut(i)*rcut(i)
write(*,*)'ntfmr',ntfmr,'i=',i,rcut(i)
enddo
.......
.......
My output is
ntfmr 3 i= 1 2.10000000000000
ntfmr 3 i= 2 2.40000000000000
ntfmr 3 i= 3 4.41000000000000
I tried to initialize rcut(i)=0. It does not work. I cannot find any place to change this value.
I am unable to figure it out what is going wrong. Any suggestion or comment will be highly appreciated.

OpenMVG mainComputeMatches.cpp input explanation

How can i compute descriptor matches using --pair_list option file (using main_ComputeMatches.cpp).
What is the format of data in the file specified by input --pair_list?
Thanks in advance for any suggestion.
You must list the image view index.
See your sfm_data.json, each image is linked to a view index.
In the pair_list file you just connect the image pair your wanna try to compute like
0 1
0 2
1 2
1 6
...
Expanding #Jack answer, you could also compact it like this:
0 1 2
1 2 6
...

Fortran95 -- Reading from a formatted text file

I need to read some values from a table. These are the first five rows, to give you some idea of what it should look like:
1 + 3 98 96 1
2 + 337 2799 2463 1
3 + 2801 3733 933 1
4 + 3734 5020 1287 1
5 + 5234 5530 297 1
My interest is in the first four columns of each row. I need to read these into arrays. I used the following code:
program ----
implicit none
integer, parameter :: totbases = 4639675, totgenes = 4395
integer :: codtot, ks
integer, dimension(totgenes) :: ngene, lend, rend
character :: genome*4639675, sign*4
open(1,file='e_coli_g_info')
open(2,file='e_coli_g_str')
do ks = 1, totgenes
read(1,100) ngene(ks),sign(ks:ks),lend(ks), rend(ks)
end do
100 format(1x,i4,8x,a1, 2(5x,i7), 22x)
do ks = 1, 100
write(*,*) ngene(ks), sign(ks:ks),lend(ks), rend(ks)
end do
end program
The loop at the end of the program is to print the first hundred entries to test that they are being read correctly. The problem is that I am getting this garbage (the fourth row is the problem):
1 + 3 757934891
2 + 337 724249387
3 + 2801 757803819
4 + 3734 757803819
5 + 5234 757935405
Clearly, the fourth column is way off. In fact, I cannot find these values anywhere in the file that I am reading from. I am using the gfortran compiler for Ubuntu 12.04. I would greatly appreciate if somebody would point me in the right direction. I'm sure it's likely that I'm missing something very obvious because I'm new at Fortran.
Fortran formats are (traditionally, there's some newer stuff that I won't go into here) fixed format, that is, they are best suited for file formats with fixed columns. I.e. column N always starts at character position M, no ifs or buts. If your file format is more "free format"-like, that is, columns are separated by whitespace, it's often easier and more robust to read data using list formatting. That is, try to do your read loop as
do ks = 1, totgenes
read(1, *) ngene(ks), sign(ks:ks), lend(ks), rend(ks)
end do
Also, as a general advice, when opening your own files, start from unit 10 and go upwards from there. Fortran implementations typically use some of the low-numbered units for standard input, output, and error (a common choice is units 1, 5, and 6). You probably don't want to redirect those.
PS 2: I haven't tried your code, but it seems that you have a bounds overflow in the sign variable. It's declared of length 4, but then you assign to index ks which goes all the way up to totgenes. As you're using gfortran on Ubuntu 12.04 (that is, gfortran 4.6), when developing compile with options "-O1 -Wall -g -fcheck=all"