Creating separate arrays from text file

I have a very long text file containing numerical data. The data looks something like this:

0.2 26 24 27 0.1 29 31 25 0.9 35 32 31 0.4 23 22 25

The first four entries correspond to four parameters: the speed and x, y, z positions of a mass during the first interval. The next four entries correspond to the speed and x, y, z positions of a mass in the second interval. My goal is to create three arrays each of which contain the values of these parameters in ascending order. For the data I provided above, the output of the program would yield three 1-D arrays:

speed = {0.2, 0.1, 0.9, 0.4}
x_pos = {26, 29, 35, 23}
y_pos = {24, 31, 32, 22}
z_pos = {27, 25, 31, 25}

I am very new to c++ and cannot figure out how to approach this problem. Any help would be greatly appreciated.
This is why XML and other markup languages are nice. Writing your own serialization object
is trivial, however depending on the size of your text file you may have a few concerns down the road.

If I had more time I would write this for you this evening. PM me if it's something you need within the next day or two.

To answer your question quickly however, you would want to brush up on a simple file stream routine using getline() and such. This site has a ton of documentation BTW.

Assuming your text file is cohesive, you would parse the values line by line (also assuming they are all in the correct order).

You could implement your own strtok() sort of routine using std::strings and split the values, using a whitespace as separator.

Then, fill your arrays, vectors, or lists with the respective values.


PS: I realize this is a C++ site, so I don't mean to diss the language, but this would be much
easier in say, Ruby, Python, or Perl. Is there any reason it has to be in C++?
1
2
3
4
5
ifstream inFile("example.dat");
const int N = 4;
double speed[N], x_pos[N], y_pos[N], z_pos[N];
for( int i = 0; i < N; ++i )
     inFile >> speed[i] >> x[i] >> y[i] >> z[i];
Last edited on
Mathhead,

This seems to work except my data file contains thousands of floating point entries. In your program you are assuming that each array will have four entries. I know that this is what I described in my example so sorry if that part was unclear. I am trying to write a program using dynamic memory allocation using the new function. In my program I count the number of entries that there should be in each parameter. I then set the number of entries equal to N. However, when I do this I get an error that says 'N must be a constant value'. Therefore, I believe I must use the new function to dynamically assign a value to N. Here is what I have so far. This program actually has 6 parameters (different than the example program I gave in the previous message). I am not quite sure how to use the new function properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	float x;
	int num_entries;
	int i;
	ifstream inFile;
	inFile.open("data.txt");

	if (!inFile) 
	{
		cout << "Unable to open file ";
		system("pause");
		exit(1); // terminate with error
	}
	//Find the number of entries for each parameter
	for (int n = 0; inFile >> x; n++)
	{
		num_entries = (n + 1)/6; //there are 6 parameters so divide the total number of entries by 6
	}
	
	cout << "number of entries is " << num_entries << endl; //outputs the number of entries for each parameter

	int N = num_entries;
	float *Energy = new float[N], float *energy_lost = new float[N], float *step_length = new float[N], float *x_pos = new float[N], float *y_pos = new float[N], float *z_pos = new float[N];
	for(int i = 0; i < N; ++i)
		inFile >> Energy[i] >> energy_lost[i] >> step_length[i] >> x_pos[i] >> y_pos[i] >> z_pos[i];
	system("pause");
	return 0;
}


Please if anyone can tell me what I may be doing wrong with the new function. I am also confused where to place the delete[] operators.
Last edited on
Mathead's example is eloquent but doesn't account for irregularities in your txt file.
I would recommend using a more dynamic approach, such as I suggested earlier.
Topic archived. No new replies allowed.