Solved

question about manipulating vectors of my objects

Posted on 2004-10-01
4
176 Views
Last Modified: 2010-04-01
Here is my code. I need help adding 3 columns from vector a to vector b.

class Column
{
public:
     Column(const string& ColumnName):m_ColumnName(ColumnName){}
     Column(const string& ColumnName, const vector<string>& ColumnData):m_ColumnName(ColumnName), m_ColumnData(ColumnData){}
     string m_ColumnName;
     vector<string> m_ColumnData;
};
typedef vector<Column> BigFileColumnVector;
void loadBigFile(const char* fileName, BigFileColumnVector& FileData);
void saveBigFile(const char* fileName, BigFileColumnVector& FileData);
void AppendToAppPort(BigFileColumnVector& AppPortVector, BigFileColumnVector& P_LVector);

int main(int argc, char* argv[])
{

      const char* fileName_AppPort = "c:\\tmp\\f05.csv";
      const char* fileName_P_L = "c:\\tmp\\p_l.csv";
      const char* fileName_AppPort_output = "c:\\tmp\\f05_output.csv";
      BigFileColumnVector AppPortVector, P_LVector;
      loadBigFile(fileName_AppPort, AppPortVector); // load vector1 from a file1
      loadBigFile(fileName_P_L, P_LVector); // load vector2 from a file2
      AppendToAppPort(AppPortVector, P_LVector); // now I need to append 3 columns from P_LVector to AppPortVector
      saveBigFile(fileName_AppPort_output, AppPortVector); //here I save the vector to a file
      AppPortVector.

  return 0;

}


void loadBigFile(const char* fileName, BigFileColumnVector& FileData)
{
     fstream read_file(fileName, ios::in);
     string Line;
     string Field;
     getline(read_file, Line);
     stringstream ss(Line);

       while(getline(ss, Field, ','))
     {
          FileData.push_back(Column(Field));
     }
     
       while(getline(read_file, Line))
     {
          stringstream ss(Line);
          int i = 0;
          while(getline(ss, Field, ','))
          {
              FileData[i++].m_ColumnData.push_back(Field);
          }
     }
     read_file.close();

}

void saveBigFile(const char* fileName, BigFileColumnVector& FileData)
{
     //Now write it to a file
     fstream write_file(fileName, ios::out|ios::trunc );
     //First write all the column titles
     for (vector<Column>::iterator i = FileData.begin();i != FileData.end();++i)
     {
          write_file << i->m_ColumnName << ",";
     }
     write_file.seekp(-1, ios::cur);// Reverse current position to delete ',' comma
     write_file << endl; //End the line

     for(int row = 0;row < FileData[0].m_ColumnData.size();++row)
     {
          for (vector<Column>::iterator ColumnItem = FileData.begin();ColumnItem != FileData.end();++ColumnItem)
          {
               write_file << ColumnItem->m_ColumnData[row] << ",";
          }
          write_file.seekp(-1, ios::cur);// Reverse current position to delete ',' comma
          write_file << endl;
     }
     write_file.close();

}

// I need help implementing this method
void AppendToAppPort(BigFileColumnVector& AppPortVector, BigFileColumnVector& P_LVector)
{



}
0
Comment
Question by:healingtao
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12203729
If I have understood correctly, you will need this:

void AppendToAppPort(BigFileColumnVector& AppPortVector, BigFileColumnVector& P_LVector)
{
    vector<Column>::iterator it = P_LVector.begin();    
   
    AppPortVector.push_back(*(it++));    
    AppPortVector.push_back(*(it++));    
    AppPortVector.push_back(*(it++));    
}

0
 

Author Comment

by:healingtao
ID: 12204460
jaime_olivares,

This code seems to work but when I call a method to save it to a file, it crashes
void saveBigFile(const char* fileName, BigFileColumnVector& FileData)
{
     //Now write it to a file
     fstream write_file(fileName, ios::out|ios::trunc );
     //First write all the column titles
     for (vector<Column>::iterator i = FileData.begin();i != FileData.end();++i)
     {
          write_file << i->m_ColumnName << ",";
     }
     write_file.seekp(-1, ios::cur);// Reverse current position to delete ',' comma
     write_file << endl; //End the line

     for(int row = 0;row < FileData[0].m_ColumnData.size();++row)
     {
          for (vector<Column>::iterator ColumnItem = FileData.begin();ColumnItem != FileData.end();++ColumnItem)
          {
              // IT CRASHES here for the last run when it gets to the new columns added
               write_file << ColumnItem->m_ColumnData[row] << ",";
          }
          write_file.seekp(-1, ios::cur);// Reverse current position to delete ',' comma
          write_file << endl;
     }
     write_file.close();

}

I'm not sure why
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 400 total points
ID: 12204990
Dont have so much time to make a full-test right now. Please try with:

void AppendToAppPort(BigFileColumnVector& AppPortVector, BigFileColumnVector& P_LVector)
{
    vector<Column>::iterator it = P_LVector.begin();    

    AppPortVector.push_back(Column(it->m_ColumnName, it->m_ColumnData)); it++;  
    AppPortVector.push_back(Column(it->m_ColumnName, it->m_ColumnData)); it++;  
    AppPortVector.push_back(Column(it->m_ColumnName, it->m_ColumnData));  
}

0
 

Author Comment

by:healingtao
ID: 12209695
thanks jaime_olivares , that did it
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question