How to read 2 different file in loop

  • Thread starter ctaini
  • Start date
  • Tags
    File Loop
In summary, the program is designed to loop through two sets of data and print the values of i and j when they are equal. However, if the data is coming from two different files, there is an issue where the program only reads the first line of the outer loop from the first file and does not continue to the next line. It appears that the code is only checking for equality between i and j, rather than reading values from the files. To fix this issue, the code should be adjusted to properly read values from both files and compare them for equality.
  • #1
ctaini
8
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
  • #2
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.
 
  • #3
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?
 
  • #4
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
 
  • #5
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..
 
  • #6
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
 
  • #7
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.
 
  • #8
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).
 
  • #9
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.
 

Related to How to read 2 different file in loop

1. How do I read 2 different files in a loop?

To read 2 different files in a loop, you can use the while loop and the open() function in your code. First, open both files using the open() function and assign them to different variables. Then use a while loop to iterate through both files simultaneously and read the contents of each file line by line.

2. Can I use a for loop to read 2 different files?

Yes, you can use a for loop to read 2 different files. However, unlike the while loop, you will need to use the zip() function to iterate through both files simultaneously. The zip() function will combine the lines from each file into tuples, which can then be unpacked and read in your for loop.

3. How do I make sure the files are read in the correct order?

The order in which the files are read will depend on how you open them and in what order you place them in your loop. For example, if you open file A first and then file B, the loop will read file A first and then file B. To change the order, you can simply switch the order in which you open the files or use the reversed() function to reverse the order of the files in your loop.

4. What if one of the files has more lines than the other?

If one of the files has more lines than the other, the loop will stop when it reaches the end of the shorter file. This means that some lines from the longer file will not be read. To avoid this, you can use the try-except block to catch any errors when reading the longer file and continue with the loop.

5. How can I use the data from both files together in my loop?

You can use the data from both files together by storing them in a data structure such as a list or dictionary. As you iterate through the files, you can append or add the data to the data structure. Then, you can access and use the combined data from both files in your loop as needed.

Similar threads

  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top