Can't put .txt file into an array

  • Thread starter chemengstuden
  • Start date
  • Tags
    Array File
In summary, the conversation discusses a programming problem involving inputting a .txt file with variable rows and 3 columns into an array. The first row contains the remaining number of rows and the numbers are separated by spaces. The code provided shows an attempt at solving the problem using a while loop, but it is suggested to use a counting loop instead. The issue of incorrect decimal values is also addressed, and it is recommended to increase the precision to 8 bytes.
  • #1
chemengstuden
6
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
  • #2
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.
 
  • #3
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
 
  • #4
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.
 
  • #5
Never mind I figured out the problem, I have to increase the precision to 8 bit, by declaring as REAL*8::
 
  • #6
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.
 
  • #7
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.
 

FAQ: Can't put .txt file into an array

Why am I unable to put my .txt file into an array?

This could be due to a few reasons. One possibility is that your file is not formatted correctly and therefore cannot be read by the array. Another possibility is that there is an error in your code or the file path is incorrect.

How can I fix the issue of not being able to put my .txt file into an array?

First, make sure that your file is properly formatted and that the array is able to read it. Check for any errors in your code and ensure that the file path is accurate. You can also try using a different method or function to input the file into the array.

Are there any alternatives to using an array for my .txt file?

Yes, there are other data structures that can be used to store and manipulate data from a .txt file. Some examples include lists, dictionaries, and sets. It is important to choose the appropriate data structure based on the type of data and the operations you want to perform.

Can I use a .txt file with different programming languages?

Yes, .txt files are basic text files and can be used with most programming languages. However, the syntax for reading and manipulating the file may differ based on the language.

Is there a limit to the size of the .txt file that can be put into an array?

The size limit for a .txt file that can be put into an array depends on the programming language and the system's memory capacity. Generally, larger files may require more memory and may take longer to process. It is important to optimize your code and consider using other data structures for very large files.

Similar threads

Back
Top