Since i am a Ph.D student in material science, i have no previous experience in programming, and for 1 week now i am trying to write a simple program...where i can use some help from you.
I have written a program that can read every line in a txt file and put it in a seperate array.
Now the problem is to modify this program (as seen below), and make it store only the useful information and not all the other useless data from the file.
The useful information is only the one that is green in the txt below.
I can think 2 different solutions but until now i cannot implement them cause i cannot write the right conditions to select only the diserable data:
1) create 4 separate 1D arrays for each of the different number sets X1,Y1,X2,Y2 (because the 2 different X sets and Y sets are different)
2) make two 2D arrays having inside the first pair of X-Y0 and then the second one X-Y0
This is the program:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
#define MAX_LINES 250
#define MAX_IN_LINE 500
int main() {
//open our file
ifstream myfile ("test1.txt");
//initialize our array's
float myarray[MAX_LINES][MAX_IN_LINE];
//used for knowing how many numbers are stored in each array
float totalargs[MAX_LINES];
//rest of variables
string line,tempnum;
int end=0;
int firstarg=0,secondarg=0;
//set all of our arrays to be zero'd out
memset(myarray,0,MAX_LINES*MAX_IN_LINE);
memset(totalargs,0,MAX_LINES);
//make sure file is opened
if (myfile.is_open()) {
while(!myfile.eof()) {
getline(myfile,line);
//if there is a , in the line we have gotten
while((end=line.find(',',0))!=string::npos) {
//get the number before the ,
tempnum=line.substr(0,end);
myarray[firstarg][secondarg]=atof(tempnum.c_str());
secondarg++;
//erase the part of the line we have gotten
line.erase(0,end+1);
}
//we will have an extra number at the end after that loop
//this gets that last number
tempnum=line.substr(0,line.length());
myarray[firstarg][secondarg]=atof(tempnum.c_str());
//set the number of args to our array
totalargs[firstarg]=secondarg;
firstarg++;
//reset arg.
secondarg=0;
}
} else {
cout << "cannot open";
}
//this is extra, but it just shows you your variables and that
//they really do have numbers in them.
//cout << "num: " << num << endl;
for (int x=0;x<firstarg;x++) {
cout << "Array " << x+1 << ": " << myarray[x][0];
for (int y=1;y<=totalargs[x];y++) {
cout << "," << myarray[x][y];
}
cout << endl;
system("pause");
}
}
And this is the txt file:
<FTML::TextArchive>
ClassList=
{
NumClass=8
<PFLib::XYPlot tag=17896520>
Title=true "BBSEgrating $alpha measured vs theory"
Border=true
Xaxis=<PFLib::XYAxis>
Label=""
Style=
{
LogScale=false
Horizontal=true
MinTicLabel=false
DoAxisLabel=true
Grids=true false
X=
[
214.9439,215.6353,216.3268,217.0182,217.7096,218.401,219.0923,
219.7837,220.475,221.1664,221.8577,222.549,223.2403,223.9316,
224.6228,225.3141,226.0053,226.6966,227.3878,228.079,228.7702,
229.4614,230.1525,230.8437,231.5348,232.2259,232.9171,233.6081,
]
Y0=false y
[
0.3389342,0.3292327,0.3355889,0.3375014,0.3446867,0.3494859,
0.3348443,0.3376398,0.3323968,0.3430243,0.3442318,0.3256431,
0.3421532,0.3383825,0.3349358,0.3334366,0.335031,0.3346525,
0.3282507,0.3304749,0.3269976,0.3227021,0.3225013,0.3205901,
]
<\>
<\>
<PFLib::XYPlot tag=23590016>
Title=true "BBSEgrating $beta measured vs theory"
Border=true
Xaxis=<PFLib::XYAxis>
Label=""
Style=
X=
[
214.9439,215.6353,216.3268,217.0182,217.7096,218.401,219.0923,
219.7837,220.475,221.1664,221.8577,222.549,223.2403,223.9316,
224.6228,225.3141,226.0053,226.6966,227.3878,228.079,228.7702,
229.4614,230.1525,230.8437,231.5348,232.2259,232.9171,233.6081,
]
Y0=false y
[
-0.09574334,-0.0999236,-0.07834335,-0.09091064,-0.09743083,
-0.09091423,-0.09133047,-0.07846043,-0.08289151,-0.09073865,
-0.08474597,-0.06513024,-0.07882992,-0.06917978,-0.06838764,
-0.06277183,-0.06282892,-0.05313438,-0.05481997,-0.04657339,
]
<\>
<\>