qq_34023390 2016-08-09 06:11 采纳率: 0%
浏览 6609
已结题

在CSV文件中,怎样将第一个文件的数据写入第一列,将第二个文件的数据写入第二列?

图片说明

如图所示,在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;
    }

  • 写回答

5条回答

  • 鱼弦 全栈领域优质创作者 2016-08-09 08:38
    关注

    首先你得获取列数,然后 封装一个读取第一个csv 文件的 方法索取 值,便利存入 第二个文件

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题