#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;
}
从文件夹中读取文件并存放于指定文件
于 2024-01-23 15:04:08 首次发布