Importing numerical data

Hello,

I am working on a computational physics problem for my personal research (new to all of this, but I've used c++ to write small programs previously).

I have a large (HUGE, 9 values given at 128 different times.... but this was repeated 334 times) amount of data.
They are in a .txt format file like
0 3.68204e+14 4.15505e+13 -8.30486e+13 4.13776e+13 3.18056e+14 -4.62693e+13 -8.23936e+13 -4.623e+13 3.87667e+14
1 8.10475e+13 1.16273e+13 -2.31364e+13 1.15839e+13 3.01511e+13 5.39847e+12 -2.29789e+13 5.37484e+12 2.17842e+13
...
where the first is the time and then the nine values.
I need to import these values so I can analyze them. Truely you could think about each of these nine values as a matrix.
I have done a lot of reading on importing stuff, but it is never this large or in this type of file outlay.

Thanks!
One of the tricks to programming is learning how to break down seemingly large problems into smaller, more managable tasks.

I would do something like this:


First step:
Figure out how you want the information stored in your program. If each group of 9 values is like a matrix, maybe you want a Matrix class or something that represents one of them. But really, how you want this arranged depends on how you will be using the data in your program. You want to make this class/struct easy to use.

Next step:
Write a basic routine to read one such matrix from the file into an instance of your Matrix class. This would be a small and simple routine that only reads 9 values from the file.

Final step:
Loop over that above routine X times until the entire file is read.
Thank you very much for your quick reply. This is good thinking. Also, I guess I should have been more concise. How could I read from my .txt file and use these values? Preferably then store them as something.
Last edited on
Use fstream:

1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>
using namespace std;

int main()
{
  ifstream file( "yourfilename.txt" );

  double a;
  file >> a;  // reads one value from the file, puts it in 'a'
  file >> b;  // puts the next one in 'b'
  // etc
}
But is there a good way of doing this with say 427520 values? Thank you for your reply.
make an array with size 427520, and then read the data into it.
Could you explain how would would do this? I see Disch's comment, but I don't get how I would take it from file and put it in an array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include <fstream>

using namespace std;

int main() {

    double data[427520];
    
    ifstream input("file.txt");
    
    for (int i = 0; i < 427520; i++) {
        input >> data[i];
    }
}

Topic archived. No new replies allowed.