read in problem

This is a discussion on read in problem within the C++ Programming forums, part of the General Programming Boards category; hay I have a problem with the following read in procedure: Code: ifstream widthfile("data-files/output_file3.txt"); while((widthfile >> x[n] >> y[n] >> ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    5

    read in problem

    hay
    I have a problem with the following read in procedure:
    Code:
       ifstream widthfile("data-files/output_file3.txt");
       while((widthfile >> x[n] >> y[n] >> z[n] >> vx[n] >> vy[n] >> vz[n] >> mass >> type[n])){
          if(xmax < x[n]) xmax = x[n];
          if(ymax < y[n]) ymax = y[n];
          if(zmax < z[n]) zmax = z[n];
          n++;
       }  
       widthfile.close();
    this gives an error to me. If I use only 5 columns in the file output_file3.txt it is working, so the problem is connected to the number of colums... ???
    I can avoid the problem if I read only one data set and distribute it afterwards like
    Code:
       ifstream widthfile("data-files/output_file3.txt");
       while((widthfile >>dummy)){
          if(n%0 == 0){
             x[n1] == dummy;
             n1++;
         }
         ....
          n++;
       }  
       widthfile.close();
    but this is very ugly... I would like to know what is my mistake withe the procedure above.
    thanks for any help
    florian

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    There's nothing wrong with this piece of code. Could you show us the rest, including variable declarations and initialization?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    of course... here is the complete program (its a root program but I hope its not connected to root itself)

    Code:
    #include <math.h>
    
    void pos_plot_2color(){
       gROOT->SetStyle("Plain");
       gStyle->SetOptStat(0);
       gStyle->SetOptFit(1111);
    
       const int num = 1000000;
       double x[num], y[num], z[num], type[num], dummy;
       double vx[num], vy[num], vz[num];
       double xmax, ymax, zmax, mass[num];
       int j, n;
    
       xmax = 0;
       ymax = 0;
       zmax = 0;
    
       n = 0;
    
       ifstream widthfile("data-files/output_file3.txt");
       while((widthfile >> x[n] >> y[n] >> z[n] >> vx[n] >> vy[n] >> vz[n] >> mass[n] >> type[n])){
          if(xmax < x[n]) xmax = x[n];
          if(ymax < y[n]) ymax = y[n];
          if(zmax < z[n]) zmax = z[n];
          n++;
       }  
       widthfile.close();
    
       cout<<"xmax: "<<xmax<< endl;
       cout<<"ymax: "<<ymax<< endl;
       cout<<"zmax: "<<zmax<< endl;
    
       TH3F *frame3d = new TH3F("frame3d","frame3d",10,-xmax,xmax,10,-ymax,ymax,10,-zmax,zmax);
       frame3d->Draw();
    
       TPolyMarker3D *pm3d1 = new TPolyMarker3D(3);
       j = 0;
       for(int i = 0; i <= n; i++){
          if(type[i] == 2){
             pm3d1->SetPoint(j,x[i],y[i],z[i]);
             cout<<"x: "<<x[i]<<" y: "<<y[i]<<" z: "<<z[i]<<" vx: "<<vx[i]<<" vy: "<<vy[i]<<" vz: "<<vz[i]<< endl;
             j++;
          }
       }
    
       cout<<"type1: "<<j<< endl;
    
       pm3d1->SetMarkerSize(1);
       pm3d1->SetMarkerColor(kRed);
       pm3d1->SetMarkerStyle(20);
       frame3d->SetTitle("");
    
       pm3d1->Draw();
    
       TPolyMarker3D *pm3d2 = new TPolyMarker3D(3);
       j = 0;
       for(int i = 0; i <= n; i++){
          if(type[i] == 1){
             pm3d2->SetPoint(j,x[i],y[i],z[i]);
             if(x[i] == 10000. && y[i] == 0. && z[i] == 0.) cout<<"i = "<<i<< endl;
             j++;
          }
       }
    
       cout<<"type2: "<<j<< endl;
    
       pm3d2->SetMarkerSize(0.1);
       pm3d2->SetMarkerColor(1);
       pm3d2->SetMarkerStyle(20);
    
       pm3d2->Draw();
    }
    thanks fpr the help
    florian

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You allocate on the stack:
    8 arrays
    1000000 elements each
    8 bytes per element

    That makes up for 64 MB. WAY too big for the stack; on the heap it should be OK if there's really no better way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem of read file in C++
    By rosicky2005 in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2006, 05:25 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 07:02 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21