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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛