Solved

question about manipulating vectors of my objects

Posted on 2004-10-01
4
175 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path to  STL Map header file 1 70
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 67
object oriented programming comparison 5 77
Outlook 13 45
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 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