How to read 2 different file in loop

  • Thread starter Thread starter ctaini
  • Start date Start date
  • Tags Tags
    File Loop
AI Thread Summary
The discussion focuses on a Fortran programming issue where the user is trying to read data from two different files in nested loops. The outer loop reads from "one.txt" while the inner loop reads from "two.txt", but the program fails to continue to the next iteration of the outer loop after processing the inner loop. The user is encountering a problem where the program only reads the first line of "one.txt" and does not proceed to subsequent lines. Suggestions include printing debug information to track the program's flow and ensuring that the reading logic correctly handles the end-of-file conditions for both files. The user seeks a solution to enable the program to iterate through all lines of both files correctly.
ctaini
Messages
8
Reaction score
0
hi...

program below just for example

integer i,j
do j=1,100
do i=55,78
if(i==j)then
print*,i,j
end if
end do
end do
end

if base on do loop theory,outer do will read 1st line of j then proceed to inner loop,then find if i==j,if true the program will print result and if false the program will proceed to outer loop to read 2nd line and so on until outer line (j=100).

my problem is, if data for outer loop from file "one.txt" (for example )and inner loop from another file "two.txt"(for example)..i facing problem where after inner loop read all the data,the program will end at inner loop,program not proceed to outer loop.The program only read 1st line of outer loop from "one.txt" file.
just for info,both file contain different format and line.

example
file "one.txt"
a1 b1
1004 4567
1015 4567
1027 4567
1038 7890
1049 7890
1055 7890
1066 7890
1074 4567
108 4567
109 7890
110 7890

file"two.txt"
c1 b2
440 1056
458 0089
563 4567
789 2045
123 7890
456 2345

for example that is my file.if i make a program:

if(b1==b2)then
print*,a1,b1,c1
end if

my result is: 1004 4567 563
the result that i want is:
1004 4567 563
1015 4567 563
...
...
110 7890 123

my question is,how to make program that can complete the rule of do loop with 2 different file.
 
Technology news on Phys.org
I put [ code] and [ /code] tags (without the leading spaces) around your code, and cleaned up the indentation.
ctaini said:
hi...

program below just for example
Code:
integer i,j
do j=1,100
   do i=55,78
      if(i==j)then
         print*,i,j
      end if
   end do
end do
end
if base on do loop theory,outer do will read 1st line of j then proceed to inner loop,then find if i==j,if true the program will print result and if false the program will proceed to outer loop to read 2nd line and so on until outer line (j=100).
The loops don't "read" values of j and i. The way this works is that j is set to 1, then i is set to 55. Since i and j aren't equal, nothing is printed. Then i is set to 56, but i and j still aren't equal, so nothing is printed. When j is 1, no value of i will be equal to 1, so nothing is printed.

Next, j is set to 2, but none of the values of i are equal to 2, so nothing is printed.
Next, j is set to 3, and as before, nothing is printed.

The only time something is printed is when i == j, so you will get output when j == 55 and i == 55, and when j == 56 and i == 56, and so on through j == 78 and i == 78.

ctaini said:
my problem is, if data for outer loop from file "one.txt" (for example )and inner loop from another file "two.txt"(for example)..i facing problem where after inner loop read all the data,the program will end at inner loop,program not proceed to outer loop.The program only read 1st line of outer loop from "one.txt" file.
I don't understand this at all. The code you show above doesn't have any statements to read from a file.
ctaini said:
just for info,both file contain different format and line.

example
file "one.txt"
a1 b1
1004 4567
1015 4567
1027 4567
1038 7890
1049 7890
1055 7890
1066 7890
1074 4567
108 4567
109 7890
110 7890

file"two.txt"
c1 b2
440 1056
458 0089
563 4567
789 2045
123 7890
456 2345

for example that is my file.if i make a program:
Code:
if(b1==b2)then
   print*,a1,b1,c1
end if
my result is: 1004 4567 563
the result that i want is:
1004 4567 563
1015 4567 563
...
...
110 7890 123

my question is,how to make program that can complete the rule of do loop with 2 different file.
 
thanks for your response..

kindly refer my example for file one.txt,two.txt and program

file "one.txt"

aaaaaaaaa
bbbbbbbbb
ccccc cccc
dd dd dd dd
DATA_ONE
1004 4567
1015 4567
1027 4567
1038 7890
1049 7890
1055 7890
1066 7890
1074 4567
1081 4567
1092 7890
1103 7890
END

file "two.txt"

example
ssssssss
ddddddd
blackf
DATA_TWO
440 1056
aaa
jjj
kkk
DATA_TWO
458 1089
bbb
DATA_TWO
563 4567
ccc
DATA_TWO
789 2045
ddd
iii
DATA_TWO
123 7890
eee
DATA_TWO
456 2345
ggg
hhh
jjj
END

my code

program baca
implicit none
integer a1,b1,c1,b2,status,i
integer a1_Data,b1_Data,c2_Data
character (len =20) string1
dimension a1_Data(100)
dimension b1_Data (100)
dimension c2_Data(100)
open (unit = 1, file = "one.txt", form = "formatted")
open (unit = 2, file = "two.txt", form = "formatted")
open(unit=3,file="result.txt")
do
read (1,10,end = 30) string1
if(string1=="DATA_ONE")then
do i = 1,100
read (1,20,iostat=status)a1,b1
if (status>0)stop "c"
if (status<0)stop "b"
a1_Data (i) = a1
b1_Data (i) = b1
do
read (2,10,iostat=status)string1
if (status>0)stop "c"
if (status<0)stop "d"
if (string1=="DATA_TWO")then
read (2,20,end=30)c1,b2
if(b1_Data(i)==b2)then
c1_Data(i)=c1
print*,b1_Data(i),a1_Data (i),c1_Data(i)
write(3,*)b1_Data(i),a1_Data (i),c1_Data(i)
end if
end if
end do
end do
end if
end do
10 format (A)
20 format (i6,i6)
30 end

the result that i got:
4567 1004 563
and the program out at d (if (status<0)stop "d").

why my program not continue to outer loop?did my program wrong?can u help me solve this problem?
 
Your code is extremely difficult to read. In the future, please put your code inside [ code] and [ /code] tags (without the spaces). I have also indented your code, so that it can be more easily understood.



Code:
program baca
implicit none
integer a1,b1,c1,b2,status,i
integer a1_Data,b1_Data,c2_Data
character (len =20) string1
dimension a1_Data(100)
dimension b1_Data (100)
dimension c2_Data(100)
open (unit = 1, file = "one.txt", form = "formatted")
open (unit = 2, file = "two.txt", form = "formatted")
open(unit=3,file="result.txt")
do 
   read (1,10,end = 30) string1 
   if(string1=="DATA_ONE")then
      do i = 1,100 
        read (1,20,iostat=status)a1,b1 
        if (status>0)stop "c"
        if (status<0)stop "b" 
        a1_Data (i) = a1
        b1_Data (i) = b1 
        do 
           read (2,10,iostat=status)string1
           if (status>0)stop "c"
           if (status<0)stop "d"
           if (string1=="DATA_TWO")then
              read (2,20,end=30)c1,b2
              if(b1_Data(i)==b2)then
                  c1_Data(i)=c1
                  print*,b1_Data(i),a1_Data (i),c1_Data(i)
                  write(3,*)b1_Data(i),a1_Data (i),c1_Data(i)
             end if
           end if
        end do
      end do
   end if
end do 
10 format (A)
20 format (i6,i6)
30 end
 
thank you for your comment..;>

Code:
program baca
implicit none
integer a1,b1,c1,b2,status,i
integer a1_Data,b1_Data,c1_Data
character (len =20) string1
dimension a1_Data(100)
dimension b1_Data (100)
dimension c1_Data(100)
open (unit = 1, file = "one.txt", form = "formatted")
open (unit = 2, file = "two.txt", form = "formatted")
open(unit=3,file="result.txt")
do 
   read (1,10,end = 30) string1 
   if(string1=="DATA_ONE")then
      do i = 1,100 
        read (1,20,iostat=status)a1,b1 
        if (status>0)stop "c"
        if (status<0)stop "b" 
        a1_Data (i) = a1
        b1_Data (i) = b1 
        do 
           read (2,10,iostat=status)string1
           if (status>0)stop "c"
           if (status<0)stop "d"
           if (string1=="DATA_TWO")then
              read (2,20,end=30)c1,b2
              if(b1_Data(i)==b2)then
                  c1_Data(i)=c1
                  print*,b1_Data(i),a1_Data (i),c1_Data(i)
                  write(3,*)b1_Data(i),a1_Data (i),c1_Data(i)
             end if
           end if
        end do
      end do
   end if
end do 
10 format (A)
20 format (i6,i6)
30 end

please help me solve this problem..i already waste a week to figure out why..
thanks again for your response..
 
file "one.txt"

Code:
aaaaaaaaa
bbbbbbbbb
ccccc cccc
dd dd dd dd
DATA_ONE
1004 	4567
1015 	4567
1027 	4567
1038 	7890
1049 	7890
1055 	7890
1066 	7890
1074 	4567
1081 	4567
1092 	7890
1103 	7890
END

file "two.txt"
Code:
example
ssssssss
ddddddd
blackf
DATA_TWO
 440    1056
aaa
jjj
kkk
DATA_TWO
 458    1089
bbb
DATA_TWO
 563    4567
ccc
DATA_TWO
 789    2045
ddd
iii
DATA_TWO
 123    7890
eee
DATA_TWO
 456    2345
ggg
hhh
jjj
END
 
I'm not familiar with this language, but have you tried printing each item it is working with?

Try printing the items it's comparing as it's working so you can see exactly what it's doing.
 
thanks for your response jarednjames

-i already try separately..and its work.
**but the problem show when i combine the program..
// the program only read 1st line of file"one.txt" (1004 4567)
// then proceed to file "two.txt". if found 4567, program will print 1004 4567 563
// after read all line at file two, program will end at file two...
**program not proceed to outer loop to read the 2nd line at "one.txt"(1015 4567).
 
Mark44 said:
Code:
program baca
implicit none
integer a1,b1,c1,b2,status,i
integer a1_Data,b1_Data,c2_Data
character (len =20) string1
dimension a1_Data(100)
dimension b1_Data (100)
dimension c2_Data(100)
open (unit = 1, file = "one.txt", form = "formatted")
open (unit = 2, file = "two.txt", form = "formatted")
open(unit=3,file="result.txt")
do 
   read (1,10,end = 30) string1 
   if(string1=="DATA_ONE")then
      do i = 1,100 
        read (1,20,iostat=status)a1,b1 
        if (status>0)stop "c"
        if (status<0)stop "b" 
        a1_Data (i) = a1
        b1_Data (i) = b1 
        do 
           read (2,10,iostat=status)string1
           if (status>0)stop "c"
           if (status<0)stop "d"
           if (string1=="DATA_TWO")then
              read (2,20,end=30)c1,b2
              if(b1_Data(i)==b2)then
                  c1_Data(i)=c1
                  print*,b1_Data(i),a1_Data (i),c1_Data(i)
                  write(3,*)b1_Data(i),a1_Data (i),c1_Data(i)
             end if
           end if
        end do
      end do
   end if
end do 
10 format (A)
20 format (i6,i6)
30 end

Here's what this program (written in Fortran) does.
1. It opens the three files, one.txt, two.txt, and result.txt.
2. The outer loop starts.
...string1 is read from one.txt (which sets string1 to "aaaaaaaaa")
...if string1 == "DATA_ONE", the counting do loop starts (the strings are not equal)
3. The outer loop starts another iteration
...string1 is read from one.txt (which sets string1 to "bbbbbbbbb")
...if string1 == "DATA_ONE", the counting do loop starts (the strings are not equal)
4. The outer loop starts another iteration
...string 1 is read from one.txt (setting string1 to "ccccc cccc"
...if string1 == "DATA_ONE", the counting do loop starts (the strings are not equal)
5. The outer loop starts another iteration
...string 1 is read from one.txt (setting string1 to "dd dd dd dd"
...if string1 == "DATA_ONE", the counting do loop starts (the strings are not equal)
6. The outer loop starts another iteration
...string 1 is read from one.txt (setting string1 to "DATA_ONE"
...if string1 == "DATA_ONE", the counting do loop starts (the strings are equal)
...a) i is set to 1. a1 and b1 are read from one.txt (a1 = 1004, b1 = 4567)
... If no error occurs, a1_Data (1) is set to 1004 and b1_Data (1) is set to 4567
...The inner do loop starts
...string1 is read from two.txt (setting string1 to "example")

and so on - I'm getting tired of doing this.
This is your program, presumably written by you, so you should know what it is doing. I have no idea of what you're trying to do, so I don't understand why you have the code that you show here.
 
  • #10
thanks for your respond

-data from one.txt and two.txt is actually just a example.
-the real data come from output from ls prepost.
-but the arrangement at each file are almost same.
-i give example because output from ls prepost contain a thousand line
-my situation right know is how to relate this both file.
- because if we back to the example(one.txt and two.txt). the relation between this 2 file is number 4567 and 7890.

-the output that i want from this program is almost like this:
Code:
4567    1004    563
4567    1015    563
4567    1027    563
7890    1038    123
7890    1049    123
7890    1055    123
7890    1066    123
4567    1074    563
4567    1081    563
7890    1092    123
7890    1103    123

-my problem now,is that i write a wrong program because the output that i got only the 1st line (4567 1015 563).
-is there any solution to write a program to get that output.
 
  • #11
ctaini said:
-my problem now,is that i write a wrong program because the output that i got only the 1st line (4567 1015 563).
-is there any solution to write a program to get that output.

There is, but we can't give it to you.

Not because we don't want to, but because we just don't know what you are trying to do.

You are asking how to get the output without telling us what you need to do that should result in it.

Perhaps you should write a psuedo code tooutline the main steps you need. At least then it can be compared to your actual code.
 

Similar threads

Replies
8
Views
1K
Replies
16
Views
4K
Replies
1
Views
3K
Replies
2
Views
1K
Replies
5
Views
5K
Replies
5
Views
2K
Replies
18
Views
2K
Back
Top