Help with Fortran: Reading/Sorting a List of Integers

  • Thread starter Thread starter falcon0311
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion focuses on a Fortran programming issue related to reading and sorting a list of integers from a file. The user is encountering problems where the output does not match the expected integers, and the total count is incorrect. The main concern revolves around the file format, with suggestions that the file may not be in binary format as assumed. Recommendations include removing the "form='binary'" option to read the file correctly and ensuring the file is saved in a compatible format. The user expresses frustration with the lack of guidance from their teacher and textbook on handling file formats in Fortran.
falcon0311
Messages
29
Reaction score
0
I know this isn't really physics related, but I was wondering if anyone here knows Fortran.

I'm trying to read a list of integers from a file and sort them. Well, I'm reading them and sorting them, but my problem is the list looks something like:

23
253
23
45
75
4
856
45
234
54
34
12
21

It's printing the total number of items as being 10, then it's printing this list:

0
165584
1633955692
1668244013
1697591601
1702047597
0
1731162226
0
925854305

Then it won't even write to the new file.
Here's my open statement,

open( unit = unitnum, file = infile, status = "old", action = "read", &
form = "binary", iostat = status )

I don't feel right about it, and if I try to change the form=, the program doesn't read the file altogether. Any help is much appreciated, and if you need to see anything else, by all means, let me know. Thank you.
 
Physics news on Phys.org
try
open( unit = unitnum, file = infile, form = "binary" )
do i = 1,13
read(unit, 10 ) A(i)
enddo
10 format (F10.4)

is the file binary? I would suspect that your trying to read a file incorrectly. Try rewriting the file and saving in a *.dat naming convention. it has been a while since I wrote fortran, so my skills are rusty.
 
The binary aspect was my main question I guess. I figured binary meant one character at a time, I want the whole number, but my teacher isn't a very understanding guy and doesn't like to explain stuff. My book doesn't even mention binary, it says formatted or unformatted, and these two make the program not work at all. Binary is the only thing I can even get a read for so far, so I'm using it.
 
Binary refers to the file format. I don't think your file is in binary. Just drop the 'form="binary",' part and you should be OK.
 
Tried that and for some reason it wouldn't read the list of integers at all.
 
Thread 'Variable mass system : water sprayed into a moving container'
Starting with the mass considerations #m(t)# is mass of water #M_{c}# mass of container and #M(t)# mass of total system $$M(t) = M_{C} + m(t)$$ $$\Rightarrow \frac{dM(t)}{dt} = \frac{dm(t)}{dt}$$ $$P_i = Mv + u \, dm$$ $$P_f = (M + dm)(v + dv)$$ $$\Delta P = M \, dv + (v - u) \, dm$$ $$F = \frac{dP}{dt} = M \frac{dv}{dt} + (v - u) \frac{dm}{dt}$$ $$F = u \frac{dm}{dt} = \rho A u^2$$ from conservation of momentum , the cannon recoils with the same force which it applies. $$\quad \frac{dm}{dt}...
Thread 'Minimum mass of a block'
Here we know that if block B is going to move up or just be at the verge of moving up ##Mg \sin \theta ## will act downwards and maximum static friction will act downwards ## \mu Mg \cos \theta ## Now what im confused by is how will we know " how quickly" block B reaches its maximum static friction value without any numbers, the suggested solution says that when block A is at its maximum extension, then block B will start to move up but with a certain set of values couldn't block A reach...

Similar threads

Replies
12
Views
3K
Replies
5
Views
5K
Replies
5
Views
2K
Replies
3
Views
4K
Replies
19
Views
6K
Replies
1
Views
3K
Back
Top