Can't put .txt file into an array

  • Thread starter Thread starter chemengstuden
  • Start date Start date
  • Tags Tags
    Array File
AI Thread Summary
The discussion revolves around reading a .txt file containing a variable number of rows and three columns into an array in a programming context. The initial row indicates the number of subsequent rows, and the user is facing issues with significant figures in the output. It is suggested that using a counting loop instead of a while loop would be more effective, as the number of rows is known from the first value. The user resolved the precision issue by changing the data type to REAL*8, which allows for greater accuracy in numerical representation. Overall, the conversation emphasizes proper file reading techniques and data type management in programming.
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.
 
Back
Top