如何将向量转换为const char *

时间:2014-06-24 11:34:28

标签: visual-c++

我想解析一个txt文件,应该写在excel文件中我有图书馆乐趣excel参数需要const char * im使用向量解析文件并存储数据并传递此向量有趣它给出错误类型转换错误它显示我的代码:

#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

1 个答案:

答案 0 :(得分:1)

data[row][col]std::string,因此data[row][col].c_str()会为您提供您正在寻找的const char*

但是如果sheet是Excel工作表COM对象,则需要将字符串包装在bstr_t或_variant_t中。

BTW,在我的Excel自动化代码中,我没有在_Worksheet中看到Cell方法。