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,                        
  
        ]
    <\>
<\>

Both of your thoughts are useful.
1>If you are using this data as independent units i,e when you are using the data set by set and not as a dependent quantity then you can use the single dimension arrays for your work.
2>If in case you are using the data as dependent units, i.e if X1 and Y1 go together then you can use them in a 2d array for easy usage.

Thank you csurfer , i totally agree with you but the problem in my case is that i haven't seen programming ever in my life before, and trying writing the right conditions in order to choose only the useful data that i want (with either way) is very difficult. I dont know how to distinguish only the useful data and put it in an array!!!

Thank you csurfer , i totally agree with you but the problem in my case is that i haven't seen programming ever in my life before, and trying writing the right conditions in order to choose only the useful data that i want (with either way) is very difficult. I dont know how to distinguish only the useful data and put it in an array!!!

Hey you are doing great with your weeks preparation ok.So there is no need to panic.Well all you need to know about reading a file and reading from a particular position is given here. With this information all that you need to know is two things.
1>The place where you have your specific data.
2>The array index in which you want to place it.

As you are the file creator you can find some particular character or a particular place where you can find the data (something like a marker).If no such thing is there then follow the brute force approach and go on reading the file until a particular marker is found which marks the beginning of data.
Then create a for loop for the array input and read the specific data.

Go through the link mentioned earlier.Especially the seekg part in detail.I am sure you'll come up with something.Still if you get some problems then you can always some back to Daniweb for help.

Hello
I am most experienced in programming in PHP (Close to C language).
So here is my suggestion to your problem.
You will have to replicate the program for the language you need.

/*
 * You can find the meaning of each function on php.net
 * 
 */
    while(!$myfile.eof())
    {
        $line = getline(myfile,line);
        /* Counts the number of ,
         * If the number is not equal to 5 then jump too next line
         */
        if(substr_count($line,',') != 5)
            continue;
        $linearray = strtok($line,',');
        foreach($linearray as $line)
        {
            //TODO What ever you want to do with the data
        }
    }
commented: no -4

I tried to write the code using seekg in order to find the "[" sign in the text file and read the numbers until it finds again the "]" putting them in an array, but i couldnt make it...my knowledge in programming is too short...
it would be great if someone can help me a bit more.....

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,
]

Thanks in advance!!!

With a little change to my code it should work.

while(!$myfile.eof())
    {
        $line = getline(myfile,line);
        /* Counts the number of ,
         * If the number is not equal to 5 then jump too next line
         */
        if(substr_count($line,',') > 5)
            continue;
        $linearray = strtok($line,',');
        foreach($linearray as $line)
        {
            //TODO What ever you want to do with the data
        }
    }

Well you wont get complete code here on Daniweb.You need to try it yourself ok.This segment gives you an idea as to how to find the first"[" in the file now develop this idea further.

char c;
ifstream file_ptr;
file_ptr.open (<filename>);
while(file_ptr.good())
{
     c = file_ptr.get(); 
     if (c=='[') break;
}
Member Avatar for iamthwee
#include <fstream>
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    //read in file
    ifstream read ("test1.txt");
    
    string line;
    
    int pass = 0;
    int counter = 0; 
    int ref = 0; //can be used to reference array
    
    vector <string> tmp; 
    
    while ( getline ( read, line, '\n' ))
    {
      if ( line == "        [" )
      {
        pass = 1;
      }
      
      if ( line == "        ]" )
      {
        pass = 0;
        counter++;
        if (counter %2 == 0)
        {
          ref++; 
        }
      }
      
      if (( pass == 1 ) && ( line != "        ["))
      { 
         cout << line <<endl;
         cout << ref <<endl; //can be used to reference array
      }
    }
    
    read.close();
    cin.get();
}

Then you could use a class to access your data?

class Foo
{
public:
     vector <double> x;
     vector <double y;
}

Maybe maybe?

This is not a solution to your question, I only want to show you how your code would look like in more decent C++, so I basically just changed your code.

#include <iostream>
#include <fstream>
#include <string>
[B]#include <cstdlib>[/B] // #include <stdlib.h>

using namespace std;
[B]const int[/B] MAX_LINES [B]=[/B] 250 // #define MAX_LINES 250
[B]const int[/B] MAX_IN_LINE [B]=[/B] 500 // #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([B]getline(myfile,line)[/B]) { // !myfile.eof()
	  //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;
	[B]cin.get();[/B] //system("pause");
  }
}

The red parts are the lines of code you wrote.
The green parts are the parts how the red lines look in decent C++.
I hope I didn't miss somthing.

Further I want to provide some links:

commented: You are always right. +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.