Help with Fortran: Reading/Sorting a List of Integers

  • Thread starter Thread starter falcon0311
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary

Homework Help Overview

The discussion revolves around a Fortran programming issue related to reading and sorting a list of integers from a file. The original poster describes encountering unexpected output when attempting to read a binary file format, which does not align with the expected list of integers.

Discussion Character

  • Exploratory, Assumption checking, Problem interpretation

Approaches and Questions Raised

  • Participants explore the implications of using binary file format versus formatted input. There are questions about the correctness of the file format and whether the original poster's understanding of binary reading is accurate.

Discussion Status

Some participants have suggested dropping the binary format to resolve the reading issue, while others have expressed uncertainty about the file's actual format. The original poster has attempted various approaches but continues to face challenges in reading the integers correctly.

Contextual Notes

The original poster mentions a lack of clarity from their teacher regarding the topic and notes that their textbook does not adequately cover binary file handling, which may be contributing to their confusion.

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.
 

Similar threads

Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 1 ·
Replies
1
Views
4K