从文件夹中读取文件并存放于指定文件

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <io.h>


using namespace std;


//用于获取指定文件夹下所有文件的绝对路径
void getFiles(string path, vector<string>& files)
{
    //文件句柄  
    long  long  hFile = 0;

    //文件信息  
    struct _finddata_t fileinfo;

    string p;

    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) //"\\*"
    {
        do
        {
            //如果是目录,迭代之,如果不是,加入列表  
            if ((fileinfo.attrib & _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(p.assign(path).append("/").append(fileinfo.name), files);    //"\\"
            }
            else
            {
                files.push_back(p.assign(path).append("/").append(fileinfo.name));    //"\\"
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}
int  main()
{
    //保存数据文件夹的绝对路径
    string root = "D:/gen";
    int j = 1;
    int i;
    fstream ofs;
    string a[5][9];
    string line;
    a[0][0] = "RAW_WIDTH";
    a[0][1] = "RAW_THICKNESS";
    a[0][2] = "QUALITY";
    a[0][3] = "NO_OF_PARTS";
    a[0][4] = "TOTAL_IDLE";
    a[0][5] = "TOTAL_MARKING";
    a[0][6] = "TOTAL_BURNING";
    a[0][7] = "TOTAL_BLASTING";
    a[0][8] = "TOTAL_MARK_BLAST";
    string str = "RAW_WIDTH=";
    string str1 = "RAW_THICKNESS=";
    string str2 = "QUALITY=";
    string str3 = "NO_OF_PARTS=";
    string str4 = "TOTAL_IDLE=";
    string str5 = "TOTAL_MARKING=";
    string str6 = "TOTAL_BURNING=";
    string str7 = "TOTAL_BLASTING=";
    string str8 = "TOTAL_MARK_BLAST=";
    //保存数据的新文件
    ofs.open(".\\result.txt", ios::out);

    vector<string> files;

    cout << "开始获取文件名称!" << endl;

    //获取该路径下的所有文件的绝对路径
    getFiles(root, files);

    cout << "文件名称获取完成!" << endl;

    for (vector<string>::iterator it = files.begin(); it != files.end(); it++)
    {
        ifstream myfile(*it); // 打开要读取的文件
        if (myfile.is_open()) { // 判断文件是否打开成功
            while (getline(myfile, line)) { // 读取每一行
            
                if (line.find(str) != std::string::npos) { // 匹配查找字符
                    string c = line.substr(10, 100);
                    a[j][0] = c;
                
                }
                if (line.find(str1) != std::string::npos) { // 匹配查找字符
                    string c = line.substr(14, 100);
                    a[j][1] = c;
                }
                if (line.find(str2) != std::string::npos) { // 匹配查找字符
                    string c = line.substr(10, 100);
                    a[j][2] = c;
                }
                if (line.find(str3) != std::string::npos) { // 匹配查找字符
                    string c = line.substr(12, 100);
                    a[j][3] = c;
                }
                if (line.find(str4) != std::string::npos) { // 匹配查找字符
                    string c = line.substr(11, 100);
                    a[j][4] = c;
                }
                if (line.find(str5) != std::string::npos) { // 匹配查找字符
                    string c = line.substr(14, 100);
                    a[j][5] = c;
                }
                if (line.find(str6) != std::string::npos) { // 匹配查找字符
                    string c = line.substr(14, 100);
                    a[j][6] = c;
                }
                if (line.find(str7)  != std::string::npos) { // 匹配查找字符
                    string c = line.substr(15, 100);
                    a[j][7] = c;
                }
                if (line.find(str8) != std::string::npos) { // 匹配查找字
                    string c = line.substr(17, 100);
                    a[j][8] = c;
            
                }
            }
        } j++;
        
    }

    for (int jj = 0; jj < 4; jj++)
    {
        string temp;
        for (int kk = 0; kk < 9; kk++)
        {
            temp += a[jj][kk]+',';
        }

        cout << temp << endl;
        ofs << temp<< endl; // 向文件写入数据
        temp = "";
    }
    //ofs.close();
 //   cout << "操作完成!" << endl;
 //   system("pause");

    //for (int n = 0; n < ; n++)
    //{
    //    string temp;
    //    for (int m = 0, m < ; m++)
    //    {
    //        string temp+=a[m][n]
    //    }
    //}

    //ofs << "****************************";
    //for (int i = 0; i < 9; i++) {
    //    for (int j = 0; j < 5; j++) {
    //        cout << "运行成功" << endl;
    //        ofs << a[j][i] << ','; // 向文件写入数据
    //        //ofs << "2700,3000,45";
    //        cout << a[j][i] << endl;
    //    }
    //    cout << endl;
    //}
    ofs.close();
    cout << "操作完成!" << endl;
    system("pause");
    return 0;
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值