Wrong results when reading a txt file into an allocatable array using Fortran 90

Isaac

I'm trying to solve problems related to retaining values, when I use de/allocate in the code shown below (fortran), making a copy array, but the problem persists. I've already seen links related with the topic:

Fortran array automatically growing when adding a value

How to get priorly-unknown array as the output of a function in Fortran

It would be easy and it doesn't make sense (for the purpose of this code) if I know array dimension (input from a txt file).

Possibly I make some mistakes (one of them is obvious: minute dimension against expected total dimension). I will be grateful if someone specify them. Despite of this I can't understand how making a copy array can solve the problem, because I need to de/allocate both the temporary and the main variables.

So, is it possible to read a txt without "variable dimension" info using reallocate (de/allocate)?

That's the code (using f90):

program prueba
    implicit none
    integer, dimension(:), allocatable :: minuto, temp
    integer :: iounit, ierr
    integer :: i = 1
    integer :: n = 1

    open(newunit = iounit, file = 'datos.txt')
    read(iounit,*)

    allocate(minuto(n), temp(n))
    minuto = 0; temp = 0
    !-------------------------------------------

    do
        read(unit = iounit, fmt = '(i2)',iostat = ierr) temp(i)
        if (ierr/=0) exit

            if (mod(size(temp,1),5)==0) then
                deallocate(minuto)
                allocate(minuto(i))
                minuto((i-4):i) = temp((i-4):i)
            end if
        i = i+1
        deallocate(temp)
        allocate(temp(i))
    end do

    close(iounit)

print*,minuto

end program prueba

(I know better ways to achieve the same goal, that's only an exercise to deepen)

I use this data example (from a txt):

min
 5
10
15
20
25
30
35
40
45
50
55
 0

That's the result:

-2144186072 1 -2144186072 1 25 0 35 40 45 50

jack

In the reallocation process you deallocate minuto and don't save its old data.

This is a sample program which could work out for you

program prueba
  implicit none

  integer, allocatable :: minuto(:)
  integer, parameter   :: n = 2
  integer              :: iounit, ierr, temp(n), i

  open (newunit = iounit, file = 'datos.txt')
  read (iounit, *)

  ! init minuto. needed for move_alloc in first call
  allocate (minuto(0))

  i = 1
  do
    read (unit = iounit, fmt = '(i2)', iostat = ierr) temp(i)

    ! exit loop. still save temp(1:i-1)
    if (ierr /= 0) then
      if (i > 1) call save_temp(i-1)
      exit
    end if

    ! save all of temp
    if (i == n) call save_temp(n)

    i = mod(i, n) +1
  end do

  close (iounit)

  print *, minuto

contains
  subroutine save_temp(n_temp)
    !! append temp(1:n_temp) to minuto

    integer, intent(in) :: n_temp

    integer, allocatable :: temp_reloc(:)

    ! save old data from minuto into temp_reloc
    call move_alloc(minuto, temp_reloc)

    allocate (minuto(size(temp_reloc) + n_temp))

    ! init first part of minuto by its old data
    minuto(:size(temp_reloc))   = temp_reloc

    ! append temp's data
    minuto(size(temp_reloc)+1:) = temp(1:n_temp)
  end subroutine
end program

Output

$  gfortran -g3 -Wall -fcheck=all a.f90 && ./a.out
           5          10          15          20          25          30          35          40          45          50          55           0

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Change in a Fortran array results in change of another when using C pointers

Send custom type using MPI and Fortran 90

code creates different different results Reading csv file and txt file - java

How to fix "undefined" array when reading a file into an array with readFile

Reading txt results 0, but has entries

Reading variable from txt file

Reading a particular word in .txt file

Reading from a txt file in Java

How to use allocatable arrays in Fortran subroutines?

R CMD SHLIB Fortran 90 file which use NetCDF

Results output to txt file by group

mapping.txt file is not getting generated when using Proguard

Reverse a String by reading from a txt file

Java program to implement Dijkstra not reading txt file

what is wrong with this Pandas and txt file code

Reading the same line from a file many times in Fortran

intent(out) and allocatable Fortran arrays: what is really done?

Inconsistent result when using Fortran function on numpy array with F2PY

Remove data in txt file after 39 results

OOPとFortran90

Fortran 90 auto(?) allocation of allocatables

Create Array and print it into TXT file

character by character reading from a txt file, and writing to another file in C?

C++: "Multi-character character constant" warning when reading from a file into char array

Renaming folder using a txt file

"Error: parse error: premature EOF" error when reading n lines from a json file using jsonlite

Bash: Unexpected parallel behavior when reading arguments from file using xargs

Reading First and Last Line of txt File; Fastest Method and Subprocess Troubleshooting

Sscanf in C - reading numbers from a .txt. file

TOP 一覧

  1. 1

    Unity:未知のスクリプトをGameObject(カスタムエディター)に動的にアタッチする方法

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    Ansibleで複数行のシェルスクリプトを実行する方法

  4. 4

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  5. 5

    Crashlytics:コンパイラー生成とはどういう意味ですか?

  6. 6

    GoDaddyでのCKEditorとKCfinderの画像プレビュー

  7. 7

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  8. 8

    Chromeウェブアプリのウェブビューの高さの問題

  9. 9

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  10. 10

    Windows 10の起動時間:以前は20秒でしたが、現在は6〜8倍になっています

  11. 11

    Reactでclsxを使用する方法

  12. 12

    ファイル内の2つのマーカー間のテキストを、別のファイルのテキストのセクションに置き換えるにはどうすればよいですか?

  13. 13

    MLでのデータ前処理の背後にある直感

  14. 14

    グラフからテーブルに条件付き書式を適用するにはどうすればよいですか?

  15. 15

    Pythonを使用して同じ列の同じ値の間の時差を取得する方法

  16. 16

    mutate_allとifelseを組み合わせるにはどうすればよいですか

  17. 17

    ネットワークグラフで、ネットワークコンポーネントにカーソルを合わせたときに、それらを強調表示するにはどうすればよいですか?

  18. 18

    テキストフィールドの値に基づいて UIslider を移動します

  19. 19

    BLOBストレージからデータを読み取り、Azure関数アプリを使用してデータにアクセスする方法

  20. 20

    PowerShellの分割ファイルへのヘッダーの追加

  21. 21

    ソートされた検索、ターゲット値未満の数をカウント

ホットタグ

アーカイブ