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:
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++?
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.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
usingnamespace 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 = newfloat[N], float *energy_lost = newfloat[N], float *step_length = newfloat[N], float *x_pos = newfloat[N], float *y_pos = newfloat[N], float *z_pos = newfloat[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.
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.