1

How can I read a entire file into array? Say I have a big file that's just one line. How do you do that in Fortran? This does not work and I don't get why.

PROGRAM FOO
    implicit none
    character, allocatable :: input(:)
    integer :: fileSize
    open(unit=1,file='input.txt',status='old',action='read')
    read(1,*) fileSize
    allocate(input(fileSize))
    read(1,*) input
    close(1)
    print *, input
END PROGRAM FOO

EDIT: Sorry I'm new to Fortran and it's ways so a lot of things that are obvious to you when it comes to Fortran are NOT obvious to me.

Example of input:

ATTACAGAACAGTTGATCGAGCATAGTGTCGATTCACGGATCTTTCTTGCGGCCGTATAGCACTCATTGATACTTTATGTCTACGCCGGGGTATATATTAAAACACATCGCCGGTAAGCAACACGGTATGGTTATATCTGCAGGCGCATAATGTAGTCTAACATCGACCAGTGAACTCCCTGCATTGAAACTAGGTCCATGGTGCGGAATGAGTCATTCTAGGTGTCTAACGTTTGATCTAAACAAGCCGTCGGCGGCGTGCGATTTCTTAGAGCAACCCCGGAGGACGTTCCATCTTCGTATCATTCCCCGCTGACTATCTATCGCCTAATGCTCGAATCAACCAACGGCTGTGATATTCATCACATCTAGTAGATTCTACGTGCATCGGCAACATACTAGAGTTACTGAGGAGGTCCCAAGGTGAGGTCCCATGTGTCAGTTACAAAGTCCTTAGCTAGCAGTGGGCCTCGCGCTGATCTATGGCCGTTAAGTGCCCGGGCTTGTCCTCGAACTTTGACTGAGCACTCAGGGAGAGACTCCTCACCACTCTTACCGCCAGAATGTATTATGCGGACACACATAGATTGACAACCGATAAGGCGGACCAGCGGGTTATCCCAAGCTCATAGCGACTCAACTCGATGATGGAATGAGTGCTGCTTATGTCGCTCACCCCGTAGGGTTAGCGAGAATAGCGAGATAATGGGAATACGACGGAGTTAAGTAAGCGAACACGTGCTTCTGGCGCACATCCGTGACGTTACTTGGGCCCTTAGCTTCATACCAAAGCGCCGGCCGACGGAACGGGACGGTAGGAAGTAATAAGGTGTCGATAGCCCATTTCATAGGTTACTACAGGTCTTGGGCCGCCGAGCTTAGTTAGTAATGATAATAACGCGGCCGATATGATT

Suppose it's one really long line.

But also how would you handle multiple lines? Do you iterate with the isostat? And would you make a double allocable array? In Java this is kind of easy to do but in Fortran I just don't get it. Which is why I'm asking. Again I'm really new to Fortran so please understand that and it's the reason I'm asking.

boardkeystown
  • 180
  • 1
  • 11
  • 3
    This is almost certainly a duplicate, but to pick the best one it would help if you told us the file format. The above should work if on the first line of the file is the number of elements you want to read, and then on the next and subsequent lines is the data, with no other extraneous stuff. So what, *exactly*, is the file format. Please edit the question show a short version with, for example, 10 elements of input to be read. – Ian Bush Sep 02 '21 at 07:41
  • 3
    You should really first tell us, what exactly is supposed to be in that file. OTOH *never* say "this does not work". Instead, tell us what happens. Do you get any error message? Which ones exactly? Is the data you received incorrect? How exactly? What did you receive? What exactly did you receive instead? Please se [ask] and [mcve]. – Vladimir F Героям слава Sep 02 '21 at 07:58
  • 3
    Be advised that it is risky to use unit numbers smaller than 10. They often serve special.purposes. Most risky are typically 0, 5 and 6, but that is compiler dependent. If the number of elements is on the same line as the rest of the data, you will have to read it slighly differently, e.g. with non-advancing IO. Please show use the actual file content. – Vladimir F Героям слава Sep 02 '21 at 08:02
  • @VladimirF I didn't know that. Makes sense I will keep that in mind. – boardkeystown Sep 04 '21 at 17:10
  • 2
    Does this answer your question? [Reading a character string of unknown length](https://stackoverflow.com/questions/14765382/reading-a-character-string-of-unknown-length) – veryreverie Sep 04 '21 at 17:32
  • 1
    The link provided by veryreverie solves the main question. It even contains a ready-to-compile function in the answers. You really have to read the content in chunks. The other questions may call for posting a new queation post. – Vladimir F Героям слава Sep 04 '21 at 18:27

0 Answers0