如图所示,在excel文件中,我想把第一个txt的数据写入excel的第一列,把第二个txt的数据写入excel的第二列中?现在我只能把两个txt文件的数据全部放在一列中,如何修改才能达到目的呢?码代如下:
#include <iostream>
#include <fstream>
#include <vector>
#include <windows.h>
#include <string>
using namespace std;
char* WcharToChar(const wchar_t* wp)
{
char *m_char;
int len= WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),NULL,0,NULL,NULL);
m_char=new char[len+1];
WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),m_char,len,NULL,NULL);
m_char[len]='\0';
return m_char;
}
wchar_t* CharToWchar(const char* c)
{
wchar_t *m_wchar;
int len = MultiByteToWideChar(CP_ACP,0,c,strlen(c),NULL,0);
m_wchar=new wchar_t[len+1];
MultiByteToWideChar(CP_ACP,0,c,strlen(c),m_wchar,len);
m_wchar[len]='\0';
return m_wchar;
}
wchar_t* StringToWchar(const string& s)
{
const char* p=s.c_str();
return CharToWchar(p);
}
int main()
{
const string fileform = "*.txt";
const string perfileReadPath = "Data";
const int perclass_sample = 20;
string fileReadPath;
int nLine = 0;
string buf;
vector<float> descriptors;
int j = 0;
ofstream file("data_file.csv");
fileReadPath = perfileReadPath + "/" + fileform;
HANDLE hFile;
LPCTSTR lpFileName = StringToWchar(fileReadPath);
WIN32_FIND_DATA pNextInfo;
hFile = FindFirstFile(lpFileName, &pNextInfo);
if(hFile == INVALID_HANDLE_VALUE)
{
exit(-1);
}
do
{
if(pNextInfo.cFileName[0] == '.')
continue;
j++;
printf("%s\n",WcharToChar(pNextInfo.cFileName));
ifstream src_data(perfileReadPath + "/" + WcharToChar(pNextInfo.cFileName));
while( src_data )
{
if( getline( src_data, buf ) )
{
nLine++;
descriptors.push_back( atof(buf.c_str()) );
}
}
src_data.close();
for(int i=0; i<descriptors.size();i++)
{
file<<descriptors[i]<<endl;
}
//格式化容器,清除容器内所有数据,为保存下一个txt文件数据文件做准备
descriptors.clear();
} while (FindNextFile(hFile,&pNextInfo) && j<perclass_sample);
return 0;
}