Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

command do

Status
Not open for further replies.

lazic67

Technical User
Feb 23, 2011
10
RS
Hi,

I have a problem with the command DO or my code is not good.
The code is simple I want to read the file Godea.txt and write to Zbirno.txt new file only with the last line from file Godea.txt.

My code looks like this:

program ZbKr

character*20 firma
integer sat,min,sek,satdec,defsat,defmin,defsek
open (1,file='Godea.txt',status='old')
open (2,file='Zbirno.txt',status='new')

do j=1,10000
read (1,*) firma,sat,min,sek
defsat=sat
defmin=min
defsek=sek
end do
write (2,*) defsat,defmin,defsek
end program

Command DO correctly read the file but when Godea.txt be completed by the command DO does not print information on the new file Zbirno.txt, just open it.

Godea.txt file looks like this:
Godea 0 12 53
Godea 0 19 68
Godea 0 22 97
Godea 0 23 152
Godea 0 29 164
Godea 0 44 215
Godea 0 79 258
Godea 0 83 278
Godea 0 88 293
Godea 0 89 293
Godea 0 96 315
Godea 0 101 339
Godea 0 107 395
Godea 0 118 398
Godea 0 128 438
Godea 0 146 461
Godea 0 154 488
Godea 0 155 514
Godea 0 155 556
Godea 0 172 575
Godea 0 173 613
Godea 0 181 655
Godea 0 184 679
Godea 0 187 680
Godea 0 227 738
Godea 0 240 764
Godea 0 258 797
Godea 0 323 1242
Godea 0 343 1248
Godea 0 343 1304
Godea 0 347 1306
Godea 0 348 1330
Godea 0 352 1373
Godea 0 357 1401
Godea 0 358 1448
Godea 0 360 1452
Godea 0 367 1541
Godea 0 367 1642
Godea 0 368 1689
Godea 0 391 1893
Godea 0 410 2227
Godea 0 425 2229
Godea 0 695 3805
Godea 0 709 3811
Godea 1 731 3815
Godea 1 739 3825
Godea 1 748 3873
Godea 1 750 3915
Godea 1 751 3943
Godea 1 751 3961
Godea 1 751 4010
Godea 1 751 4033
Godea 1 755 4050
Godea 1 760 4089
Godea 1 760 4123
Godea 1 760 4140
Godea 1 760 4150
Godea 1 764 4203
Godea 1 778 4252
Godea 1 785 4276
Godea 1 787 4316
Godea 1 789 4369
Godea 1 789 4420
Godea 1 795 4463
Godea 1 795 4517
Godea 1 795 4528
Godea 1 802 4633
Godea 1 807 4646
Godea 2 1018 5983
Godea 2 1032 6004
Godea 2 1051 6054
Godea 2 1055 6095
Godea 2 1057 6142
Godea 2 1070 6164
Godea 2 1081 6179
Godea 2 1100 6355
Godea 2 1146 6373
Godea 2 1161 6428
Godea 2 1202 6469
Godea 2 1257 6473
Godea 2 1293 6477
Godea 2 1309 6483
Godea 2 1310 6506
Godea 2 1319 6527
Godea 2 1333 6534
Godea 2 1405 6668
Godea 2 1534 6772
Godea 2 1575 6807

Can anyone help me?

Cheers
 
Why I are you putting do 1,10000?
You have 10000 lines in a file?
 
I have so many lines in a file I tried with the code

n=0
do j=1,10000
n=n+1
read (1,*) firma,sat,min,sek
else
end if
end do

I tried the same number of lines in a file Godea.txt but we do not go. Number n that I get from reading the number of lines in order to include

code do j=1,n
read (1,*) firma,sat,min,sek
defsat=sat
defmin=min
defsek=sek
end do
write (2,*) broj,defsat,defmin,defsek

but you can not
 

Several troubles in your code :

- don't use file units less than 10, NEVER : several of these units are pre-connected to special devices.

- you open the second file with the status "new". This is rather strict, especially when testing your code, because a second run will fail just because of the existence of that file. I suggest the use of status="unknown" which is more flexible.

- you don't check the end of file to stop the reading properly

- your write statement is outside the loop. So the second file will contains a single line corresponding to the last line of the read file.

- you don't need to duplicate the read values.

Code:
program ZbKr

  implicit none

  character(20) firma
  integer sat,min,sek
  
  open (11,file='Godea.txt',status='old')
  open (12,file='Zbirno.txt',status='unknown')

  do
    read (11,*,end=100) firma,sat,min,sek
  end do
  
  100 continue
  
  write (12,*) sat, min, sek

end program

Resulting file "Zbirno.txt"

Code:
           2        1575        6807

François Jacq
 
First to thank you for your prompt response, and spent your time.

I use Compaq Visual Fortran, when I entered this code get Zbirno.txt completely empty, nothing is written.

I can not believe it. When I was quite simple and works
great.

Which compiler you use.

 
I use any FORTRAN-95 compiler like gfortran (GCC), g95, ifort (Intel), Nagfor (Nag), lf95 (Lahey/Fujitsu) ...

But I mainly work using Linux instead of Windows. Anyway, you should get the same result with any FORTRAN-90 compiler under any operating system.

Notice that gfortran and g95 are free compilers working on most operating systems, including Windows.

Possibly, the compiler you use is not Fortran 95 (I don't remember for CVF which is a quite old and unmaintained compiler).

Possibly, the crash occurred when reading the file because the format "*" is incorrect in that case with a FORTRAN-77 compiler.

Please, add print instructions in the loop :

Code:
  do
    write(*,*) "before reading"
    read (11,*,end=100) firma,sat,min,sek
    write(*,*) "after reading",firma,sat,min,sek
  end do

We will see what happens...

Else you might try to use g95 or gfortran : these compilers are easy to install.



François Jacq
 
This is incredible, I started to doubt myself.
When I tried this at the Lahey ED4W again I got the same
a result, nothing.

With additional lines of DO loops the same result.
At the dos screen obtained correct Godea.txt read to file,
Zbirno.txt open again but nothing posts.

I will try with another compiler.
 

Are you sure that you read and write the files in the right directory ?

I would check that carefully because I did that mistake frequently.

François Jacq
 
Yes I'm sure I read in the right directory

Now, first I try to read a number of lines and the number
introduce the DO loop
 
Another possibility would be that you cannot write into the file because it is also already loaded by another tool like a text editor for instance.

François Jacq
 
Can not so I checked it, but come on if you can help me
how to count the number of lines in Godea.txt.

With this number I made ??a new loop, and then doing what I tried.

if you put the loop

broj=0
DO 100 j=1,N
broj=broj+1
if (broj.EQ.N) then
write (22,*) frima,sat,min,sek
else
end if
end do

it works but how to introduce the number N in the loop DO

I tried to make a loop

broj=0
do 150 j=1,10000
broj=broj+1
read (11,*) firma,sat,min,sek
continue
end do

Accurately count the number line but does not accept
command n = broj of new loop?

 
The normal way to count the number of lines is the programming I already proposed :

Code:
integer :: n
...
n=0
do ! infinite loop valid since Fortran-90
  read (11,*,end=100) firma,sat,min,sek 
  n=n+1
enddo
100 continue
write(*,*) n

François Jacq
 
First I want to apologize and to thank you for your help.
In what was the problem?

Home using my notebook that is running Windows 7 and Compaq Visual Fortran, while at work using Win XP SP2 installed with the same Compaq Visual Fortran compiler.

At work me your code works perfectly the first, which means that my compiler under Windows 7 is not working properly.

Thanks a lot

Best requards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top