如图所示,在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;
}
首先你得获取列数,然后 封装一个读取第一个csv 文件的 方法索取 值,便利存入 第二个文件
同时创建两个 缓冲流 读取两个文件的数据(缓冲流可以读取一整行数据你应该知道吧!!!)
然后用这个数据格式存储 List
当第一个流读一行后第二个流也读一行,并存入map,再放list中。
当第一个流换行时,第二个流也换行。(保证读取到同一行数据)
最后就会生成一个list 里面存放着map再去生成 想要的csv文件
这个有多种方法可以实现:
同时逐行读取两个CSV,并以“第一个文件的第一行 tab 第二个文件的第一行”的形式写入第三个CSV中。
然后在EXCEL里直接打开第三个CSV文件,就得到你想要的结果了。上面的tab代表制表符,你可能需要用chr(9)或其它转义形式来代替
把两个CSV在EXCEL里打开,在EXCEL的VBA里对两个SHEET取值写到第三个SHEET中。
把两个CSV在EXCEL里打开,在EXCEL里建立第三个SHEET,在其中的两列中分别用公式引用前两个表单中的列值即可。
使用第三方语言利用VBA或其它读写EXCEL的方法,从两个CSV文件中读到值后直接操作EXCEL控制在指定列中写入值。
#include
#include
#include
#include
using namespace std;
int main()
{
/*
流程:1.读取指定目录下的txt文件。(最多个数)
2.创建临时csv文件和最终结果csv文件。
3.按行读取临时csv文件和当前txt文件合并到结果csv文件中。
4.把结果csv文件拷贝到临时csv文件。
5.重复3和4的步骤,直到读取所有txt文件或者到达最大个数。
/
const string fileform = ".txt";//读取文件格式
const string perfileReadPath = "Data\";//指定读取的目录
const int perclass_sample = 20;//读取的最大个数
string fileReadPath = perfileReadPath + fileform;
string fileTemp = perfileReadPath + "temp.csv";//临时文件
string fileWritePath = perfileReadPath +"data_file.csv";//最终结果文件
WIN32_FIND_DATA pNextInfo;
HANDLE hFile = FindFirstFile(fileReadPath.c_str(), &pNextInfo);
if(hFile == INVALID_HANDLE_VALUE)
{
cout<<"文件不存在"<<endl;
exit(-1);
}
char chTemp[1024+1],chTxt[1024+1],chWrite[1024+1];
DeleteFile(fileTemp.c_str());//如果存在临时文件,则删除
int iCount=0;//文件个数
do
{
if(pNextInfo.cFileName[0] == '.')
continue;//不读取文件夹
string txtFilename = perfileReadPath + pNextInfo.cFileName;//当前读取的txt文件
FILE * srcFile,*tempFile,*desFile;
srcFile = fopen(txtFilename.c_str(),"r+");//打开当前读取的txt文件
tempFile = fopen(fileTemp.c_str(),"r+");//打开临时文件
desFile = fopen(fileWritePath.c_str(),"w+");//打开结果文件
while( !feof(srcFile) || (tempFile!=NULL && !feof(tempFile)))
{
memset(chTxt,0,1024);
memset(chTemp,0,1024);
if (tempFile!=NULL &&!feof(tempFile)) fgets(chTemp,1024,tempFile);//读取临时文件的整行数据
if (!feof(srcFile))
{
fgets (chTxt,1024,srcFile);//读取txt的整行数据
//-------------------------------------------
//此段代码是为了对齐格式,当临时文件读取到结尾,而txt没有读取到结尾,
//把当前的txt数据放到指定的列
if (tempFile!=NULL &&feof(tempFile))
{//临时文件不为空,但读取到结尾
for (int j = 0; j < iCount ; j++)
{
chTemp[j] = ',';
}
}
//--------------------------------------------
}
if (chTxt[strlen(chTxt)-1] == '\n')
chTxt[strlen(chTxt)-1] = '\0';//如果读取到的行数据,结尾是换行,则删除
if (chTemp[strlen(chTemp)-1] == '\n')
chTemp[strlen(chTemp)-1] = '\0';//如果读取到的行数据,结尾是换行,则删除
//最终结果文件结尾不添加换行
if (!feof(srcFile) || (tempFile!=NULL && !feof(tempFile)))
{
sprintf(chWrite,"%s%s,\n",chTemp,chTxt);
}else sprintf(chWrite,"%s%s,",chTemp,chTxt);
fputs(chWrite,desFile);//写入结果文件
}
if (srcFile != NULL) fclose (srcFile);//关闭文件
if (tempFile != NULL) fclose (tempFile);//关闭文件
if (desFile != NULL) fclose (desFile);//关闭文件
CopyFile(fileWritePath.c_str(),fileTemp.c_str(),FALSE);// 把结果csv文件拷贝到临时csv文件。
iCount++;//文件个数
}while (FindNextFile(hFile,&pNextInfo) && iCount<perclass_sample);
DeleteFile(fileTemp.c_str());//删除临时文件
return 0;
}
不会这个,可以给你思路
如果行数相同,那么打开两个TXT文件,循环读取时候同时移动指针
open fiel1.txt
open file2.txt
where{循环时候同时利用指针读取两个文件相同条数的记录
read file1.txt 记录指针N
read file2.txt 记录指针N
处理两个一条一列记录为新文件一条两列记录
}
close file1.txt
close file2.txt