Can't put .txt file into an array

  • Thread starter Thread starter chemengstuden
  • Start date Start date
  • Tags Tags
    Array File
Click For Summary

Discussion Overview

The discussion revolves around reading a .txt file containing a variable number of rows and three columns into an array in a programming context. Participants explore issues related to input formatting, data types, and reading methods in their code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to read a .txt file into an array, noting that the first row contains a single integer indicating the number of subsequent rows.
  • Another participant suggests that using different format statements for the initial read and subsequent reads may resolve issues with data types.
  • A participant shares their complete program code, indicating the structure of the array and the reading process, while also expressing concerns about the precision of the output.
  • One participant mentions trying a counting loop based on the initial integer but finds it ineffective, suggesting that iostat might be necessary to control the reading process.
  • A later reply indicates that increasing the precision to REAL*8 may resolve issues with the representation of floating-point numbers.
  • Another participant corrects the previous statement about REAL*8, clarifying that it refers to 64 bits, not 8 bits.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of using iostat for controlling the reading process, with some suggesting a counting loop as an alternative. The discussion remains unresolved regarding the best approach to handle the reading and formatting of data.

Contextual Notes

Participants note issues related to data precision and the need for appropriate data types, but there are no consensus solutions or definitive resolutions to the problems raised.

chemengstuden
Messages
6
Reaction score
0
I'm trying to input a .txt file with a variable number of rows and 3 columns into an array. The first row is just a single number expressing the remaining number of rows. so we have something like this

3
3 5.12 3
5.3 3 5
4 3 4

All the individual numbers are separated by spaces. So far I got something like this

Read(10,*)a

do while (io== 0)
read(10,*,iostat=io)array1
end do
write(*,*) a
write(*,*)array1


With "a" being a variable that contains the first number. The array fills up with the second part but the sig figs are messed up.
 
Physics news on Phys.org
chemengstuden said:
I'm trying to input a .txt file with a variable number of rows and 3 columns into an array. The first row is just a single number expressing the remaining number of rows. so we have something like this

3
3 5.12 3
5.3 3 5
4 3 4

All the individual numbers are separated by spaces. So far I got something like this

Read(10,*)a

do while (io== 0)
read(10,*,iostat=io)array1
end do
write(*,*) a
write(*,*)array1


With "a" being a variable that contains the first number. The array fills up with the second part but the sig figs are messed up.
You don't want to use the same format statement for your first READ
and subsequent READs.

In the first READ you are reading what looks to be an integer. After that it looks like you will be reading three REALs.

Instead of a while loop, using a counting loop - you know how many lines of data there are after the initial value in the text file.

There are a lot of problems with your code (such as when are you going to open your input file?). When you get something that you can run through the compiler, post that code and we'll help you out.
 
Thank you for the reply. I wanted to make my question general because, I don't want it to seem like I'm asking for answers. That's why I didn't post all the code. This is the program code



PROGRAM money
IMPLICIT NONE
REAL::array1(50,3)
integer::a
INTEGER:: c1=0,c2=0
INTEGER:: io=0
OPEN(UNIT=10, FILE= "DatabaseOfPurchases.txt")

DO c1=1,50
Do c2=1,3
array1(c1,c2)=0
end do
end do

Read(10,*)a

do while (io== 0)
read(10,*,iostat=io)array1
end do
write(*,*) a
write(*,*)array1
close(10)
end program money
 
I tried doing using a counting loop using "a" as the stop variable but it didn't work, I think I need iostat to tell the program to stop reading the file. The problem with my program is that it shows a number that is 111.34 in the text as 111.339996.
 
Never mind I figured out the problem, I have to increase the precision to 8 bit, by declaring as REAL*8::
 
chemengstuden said:
I tried doing using a counting loop using "a" as the stop variable but it didn't work, I think I need iostat to tell the program to stop reading the file.
You've already worked this out, but no, you don't need to use iostat. A counting loop will work.
chemengstuden said:
The problem with my program is that it shows a number that is 111.34 in the text as 111.339996.
 
chemengstuden said:
Never mind I figured out the problem, I have to increase the precision to 8 bit, by declaring as REAL*8::
That's 8 bytes, or 64 bits, not 8 bits.
 

Similar threads

Replies
7
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
19K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K