suneetha
suneetha

Reputation: 1

How to typecast vector into const char*

I want to parse a txt file and that should be written in excel file i have library fun for excel argument takes const char* im using vectors to parse a file and storing data and passing this vector fun its giving error typecast error its showing my code:

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<sstream>

using namespace std;

vector< vector<string> >  data;


void parse(const char* name)
{
    ifstream in(name);

  string line;

  // read the file
  while ( getline(in,line,'|') ) 
  {
    vector<string> fields;
    std::stringstream    linestream(line);
    for ( int col = 0 ; col < 21 ; col++ ) 
    {
      string f;
      linestream >> f;
      fields.push_back(f);
    }
    data.push_back(fields);
  }

  // output by rows
  for ( vector< vector<string> >::size_type row = 0 ; row < data.size() ; row++ )
  {
    for ( vector<string>::size_type col = 0 ; col < data[row].size() ; col++ ) 
    {
    //  std::vector<char> copy = data;
//char * buffer = &copy[0];
         /*char *buffer = new char[data.size()];
            std::copy(data.begin(), data.end(), buffer);*/
        //buffer=static_cast<char *>(data[row]);
        //cout << buffer[row];
        cout << data[row][col];

    }
    cout << endl;
  }

  // output by columns
  /*for ( vector<string>::size_type col = 0 ; col < data[0].size() ; col++ )
  {
    for ( vector< vector<string> >::size_type row = 0 ; row < data.size() ; row++ ) 
    {
      cout << data[row][col] << ",";
    }
    cout << endl;
  }*/
}

static void example1(const char* path)
{

        sheet->Cell(row, col)->Set(data[r][c]);/i need to pass vector into this fun
}


enter code here

Upvotes: 0

Views: 175

Answers (1)

Werner Henze
Werner Henze

Reputation: 16761

data[row][col] is a std::string, so data[row][col].c_str() gives you the const char* that you are looking for.

But if sheet is an Excel sheet COM object, you need to wrap your string in a bstr_t or _variant_t.

BTW, in my Excel automation code I don't see a Cell method in _Worksheet.

Upvotes: 1

Related Questions