How to Write a 2D Array to a File in Fortran for 3D Graph Creation?

In summary, the person wants to have their array written in the file in the same way it is written, not like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • #1
jpapa
5
0
Hello,

i have a problem and i was wondering whether anyone in here can give me a hand. I want to write a 2D array to a file in format that can be read from another program in order to make a 3D graph. I'm using fortran. In few words i want to place the data in columns and rows in the file exactly as they are written to the array.

I appreciate any help and i thank you in advance.


John
 
Technology news on Phys.org
  • #2
I'm not at all clear what you want to do here. I thought for a moment that you wanted to be able to just "print" the data file on the screen or printer, but you talk about "read from another program". HOW you save the data isn't important (they will, in any case, be saved linearly). As long as your other program reads the data back in exactly the same way the file was written you will get back exactly the same 2D array and can do whatever you want with it. That's one reason why most people today use "Object Oriented Programming": with the routines for writing to and reading from a file are part of the data definition, you are sure to do it right.
 
  • #3
I'm puzzled as to where the 3D graph comes from.
 
  • #4
HallsofIvy said:
I'm not at all clear what you want to do here. I thought for a moment that you wanted to be able to just "print" the data file on the screen or printer, but you talk about "read from another program". HOW you save the data isn't important (they will, in any case, be saved linearly). As long as your other program reads the data back in exactly the same way the file was written you will get back exactly the same 2D array and can do whatever you want with it. That's one reason why most people today use "Object Oriented Programming": with the routines for writing to and reading from a file are part of the data definition, you are sure to do it right.

I give an example. Let's say i have an array like this:


1 2 3 4 5
6 7 8 9 10
11 12 13 14 15


I want to be written in the file as it is and not like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


Is that possible?


John
 

1. How do I write a 2D array to a file?

To write a 2D array to a file, you can use a combination of loops and file operations. First, open the file in write mode using a file object. Then, use nested loops to iterate through the 2D array and write each element to the file using the file object's write() method. Finally, close the file once all the elements have been written.

2. Can I write a 2D array to a file in a specific format?

Yes, you can write a 2D array to a file in a specific format by using string formatting techniques. For example, you can use the .format() method to specify the desired format for each element in the 2D array while writing to the file.

3. How do I read a 2D array from a file?

To read a 2D array from a file, you can use the same approach as writing to a file. Open the file in read mode, use nested loops to iterate through the file and store each element in a 2D array, and finally close the file once all elements have been read.

4. Can I write a 2D array to a file in different languages?

Yes, you can write a 2D array to a file in different languages by using encoding techniques. For example, you can specify the encoding type while opening the file in write mode, and then use the appropriate encoding method to convert the 2D array elements to the desired language before writing them to the file.

5. Is there a limit to the size of a 2D array that can be written to a file?

The size of a 2D array that can be written to a file depends on the memory capacity of the system. As long as the system has enough memory to store the 2D array, there is no limit to the size of the array that can be written to a file.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
4
Views
732
  • Programming and Computer Science
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
33
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
10
Views
25K
Back
Top