Thread: More Vector Help

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    More Vector Help

    Hey,

    I am using vectors to store read in Data but something screwy is going on. Here is my ReadData Function.

    Code:
    int Data::ReadData(char *name,int lines)
    {  int dumx,dumy,dumz;
       double dumf1,dumf2,dumf3,dumf4,dumf5,dumf6;
                                                                                                  
       FILE *file;
       printf("Opening File: %s\n",name);                                                                                           
       file=fopen(name,"r"); // Opens the passed filename
       if (file==NULL){perror("ERROR: ");return 1;}
       
       printf("File Opened Successfully\n");    
       printf("Writing Data From File To Vectors\n");
    
       
       
       for (int k=0;k<lines;k++);
       {
           fscanf(file,"%i %i %i %lf %lf %lf %lf %lf %lf",&dumx,&dumy,&dumz,&dumf1,&dumf2,&dumf3,&dumf4,&dumf5,&dumf6);
    
          	x.push_back(dumx); f1.push_back(dumf1); f2.push_back(dumf2); 
          	y.push_back(dumy); f3.push_back(dumf3); f4.push_back(dumf4);
            z.push_back(dumz); f5.push_back(dumf5); f6.push_back(dumf6);
            
       } 
       printf("Data Read Successfully....\n");
    
    //   Used to Debug File Read Process.....  
    //  for (int i=0; i<x.size(); i++)
    //  {
    //     printf("%i %i %i %lf\n",x.at(i),y.at(i),z.at(i),f1.at(i));
    //  }
      setCurrentVect(1);
      printf("Current Vector Set to 1\n");
      fclose(file);
      printf("File Closed\n");
       return 0;
    
    }
    Now I define a file, with 24000 lines and pass those to this function but I am only getting a vector which is size 1 and has data from the first line......am I not using push_back properly?
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are using push_back properly. Something else is doing it. Which vector has size 1? There are 9 vectors being used. How are these vectors declared? What does setCurrentVect do? How are you determining the size to be 1? What is the value of lines when you reach this function?

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208
    Quote Originally Posted by Daved
    You are using push_back properly. Something else is doing it. Which vector has size 1? There are 9 vectors being used. How are these vectors declared? What does setCurrentVect do? How are you determining the size to be 1? What is the value of lines when you reach this function?
    The only one I have checked is vector x,y,z, however, if their not working then I can assume non are, as well as the fact I need them to be working.

    I check them using this for statement

    Code:
      
    for (int i=0; i<data.z.size(); i++)
      {
           printf("%i %i %i %lf\n",data.x.at(i),data.y.at(i),data.z.at(i),data.currentVect.at(i));
      }
    Vectors are declared as follows
    Code:
     vector<int> x,y,z;
     vector<double> f1,f2,f3,f4,f5,f6;
     vector<double> currentVect;
    setCurrentVect() assigns the vector currentVect to be equal to one of the 6 f1 vectors (depdning on user input) for display purposes.

    I am not sure what you mean by the last question but in the file the first line is 1,1,1,0,0,0,0,0,0 and the print statment gives me just that.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    208
    It seems as though my loop is only running once (found out via a print statement in the loop....
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I am not sure what you mean by the last question
    I was referring to the lines parameter to the ReadData function. The code loops from 0 to lines, so if lines is 1 the loop that calls push_back will only be run once.

    I would try to narrow down the problem to a small but complete and compilable example. Maybe take the vectors and ReadData function out of the class and include a small main() to see if you can reproduce it with code that can be posted here. Doing so might expose your problem, and if not at least someone can look at all the code instead of small snippets.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You do no error checking in the read loop, are you certain that the fscanf call isn't failing after the first read perhaps? How is lines determined? If there are greater "lines" passed into the function than there are actual lines of data in the file, then you'll likely get duplicate data in your vectors.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    208
    This has all the tid-bits in it and how I am using the code.

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Data {
    
    public: 
     vector<int> x,y,z;
     vector<double> f1,f2,f3,f4,f5,f6;
     vector<double> currentVect;
    Data();
    ~Data();
    int ReadData(char *,int);
    };
    
    int main()
    {  Data data;
    
      printf("Reading Data From File....\n");
      
      data.ReadData("3D.data2",24000);
    
      printf("Data From File Loaded Successfully\n");
    
    //   Used to Debug Normalizing Process.....
      for (int i=0; i<data.z.size(); i++)
      {
           printf("%i %i %i %lf\n",data.x.at(i),data.y.at(i),data.z.at(i),data.currentVect.at(i));
      }
    
    
    }
    
    int Data::ReadData(char *name,int lines)
    {  int dumx,dumy,dumz;
       double dumf1,dumf2,dumf3,dumf4,dumf5,dumf6;
                                                                                                  
       FILE *file;
       printf("Opening File: %s\n",name);                                                                                           
       file=fopen(name,"r"); // Opens the passed filename
       if (file==NULL){perror("ERROR: ");return 1;}
       
       printf("File Opened Successfully\n");    
       printf("Writing Data From File To Vectors\n");
    
       
       
       for (int k=0;k<lines;k++);
       {
           fscanf(file,"%i %i %i %lf %lf %lf %lf %lf %lf",&dumx,&dumy,&dumz,&dumf1,&dumf2,&dumf3,&dumf4,&dumf5,&dumf6);
           printf("%i %i %i %lf %lf %lf %lf %lf %lf\n",dumx,dumy,dumz,dumf1,dumf2,dumf3,dumf4,dumf5,dumf6);
          	x.push_back(dumx); f1.push_back(dumf1); f2.push_back(dumf2); 
          	y.push_back(dumy); f3.push_back(dumf3); f4.push_back(dumf4);
            z.push_back(dumz); f5.push_back(dumf5); f6.push_back(dumf6);
            
       }
       
       printf("Data Read Successfully....\n");
    
    //   Used to Debug File Read Process.....  
    //  for (int i=0; i<x.size(); i++)
    //  {
    //     printf("%i %i %i %lf\n",x.at(i),y.at(i),z.at(i),f1.at(i));
    //  }
      printf("Current Vector Set to 1\n");
      fclose(file);
      printf("File Closed\n");
       return 0;
    
    }
    
    Data::Data(){
    }
    Data::~Data(){
    }
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    208
    Quote Originally Posted by hk_mp5kpdw
    You do no error checking in the read loop, are you certain that the fscanf call isn't failing after the first read perhaps? How is lines determined? If there are greater "lines" passed into the function than there are actual lines of data in the file, then you'll likely get duplicate data in your vectors.
    I know for a fact there are 24000 lines, and I did do error checking within the loop with that printf statement but even it only gets executed once.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
       for (int k=0;k<lines;k++);
       {
           if ( 9 != fscanf(file,"%i %i %i %lf %lf %lf %lf %lf %lf",&dumx,&dumy,&dumz,&dumf1,&dumf2,&dumf3,&dumf4,&dumf5,&dumf6) ) {
               puts ( "data missmatch.");
               break;
           }
           else 
                 printf("%i %i %i %lf %lf %lf %lf %lf %lf\n",dumx,dumy,dumz,dumf1,dumf2,dumf3,dumf4,dumf5,dumf6);
          	x.push_back(dumx); f1.push_back(dumf1); f2.push_back(dumf2); 
          	y.push_back(dumy); f3.push_back(dumf3); f4.push_back(dumf4);
            z.push_back(dumz); f5.push_back(dumf5); f6.push_back(dumf6);        
       }
    something like this could be called error checking.
    Kurt

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    208
    Even still I know that the scanf function is working fine, it is only being executed once however....I will try ZuK's idea.

    edit: tried it and it did nothing, but for the record Data Mismatch was never displayed
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
       for (int k=0;k<lines;k++);
    remove the semicolon
    Kurt

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    208
    Wow.........a semi-colon.....

    ^ enough said....

    Thanks,
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

Popular pages Recent additions subscribe to a feed