文件自动拆分
将一个文件中同一编号的数据拆分,分别放到新建的文件夹中去。
5 #include <iostream> 6 #include <string> 7 #include <sstream> 8 #include <fstream> 9 #include <vector> 10 #include <direct.h> //创建文件夹头文件 11 #include<time.h> //获取时间头文件 12 using namespace std; 13 14 15 void getFiles( string path, vector<string>& files, vector<string> &ownname) 16 { 17 /*files存储文件的路径及名称(eg. C:\Users\WUQP\Desktop\test_devided\data1.txt) 18 ownname只存储文件的名称(eg. data1.txt)*/ 19 20 //文件句柄 21 long hFile = 0; 22 //文件信息 23 struct _finddata_t fileinfo; 24 string p; 25 if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1) 26 { 27 do 28 { 29 //如果是目录,迭代之 30 //如果不是,加入列表 31 if((fileinfo.attrib & _A_SUBDIR)) 32 { /* 33 if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) 34 getFiles( p.assign(path).append("\\").append(fileinfo.name), files, ownname ); */ 35 } 36 else 37 { 38 files.push_back(p.assign(path).append("\\").append(fileinfo.name) ); 39 ownname.push_back(fileinfo.name); 40 } 41 }while(_findnext(hFile, &fileinfo) == 0); 42 _findclose(hFile); 43 } 44 } 45 46 int main() 47 { 48 49 clock_t start_time=clock(); //开始时间 50 51 int detector_seq; 52 int sequence; 53 int arrive_time; 54 55 char * filePath = "C:\\Users\\WUQP\\Desktop\\test_devided"; 56 char * addfilePath = "\\processed_data"; 57 char processedfilePath[100]; 58 strcpy_s(processedfilePath, filePath); 59 strcat_s(processedfilePath, addfilePath); 60 cout << processedfilePath << endl; 61 _mkdir(processedfilePath); 62 63 vector<string> files; 64 vector<string> ownname; 65 66 //获取该路径下的所有文件 67 getFiles(filePath, files, ownname); 68 int size = files.size(); 69 for (int i = 0;i < size;i++) 70 { 71 cout<<endl; 72 int num_receiver_times = 0; 73 string in_str; 74 75 //检查每个文件中的事件数目 76 ifstream in_File(files[i].c_str()); 77 if (in_File.is_open()) 78 { 79 cout<<files[i].c_str() <<" is open!"<< endl; 80 while (in_File >> detector_seq >> sequence >> arrive_time) 81 { 82 if(detector_seq == 0) 83 { 84 ++num_receiver_times; 85 } 86 else break; 87 } 88 } 89 in_File.close(); 90 91 //根据事件数目动态分开各事件的数据 92 cout << "the sources of this file is " << num_receiver_times << endl; 93 vector<string>* p = new vector<string>[num_receiver_times]; 94 ifstream in_File_(files[i].c_str()); 95 if (in_File_.is_open()) 96 { 97 while (getline(in_File_, in_str)) 98 { 99 for (int j=0; j<num_receiver_times; j++) 100 { 101 p[j].push_back(in_str); 102 if ( (j+1) % num_receiver_times != 0 ) 103 { 104 getline(in_File_, in_str); 105 } 106 } 107 } 108 } 109 in_File_.close(); 110 111 //分别将不同的数据写入不同的文件 112 for (int k = 0; k < num_receiver_times; k++) 113 { 114 int flag = 0; 115 char title[80]; 116 sprintf_s(title, "%s\\%s_%d.txt", processedfilePath, ownname[i].c_str(), k); 117 ofstream outfile(title, ios::ate|ios::out); 118 for (vector<string>::iterator iter = p[k].begin(); iter!= p[k].end(); iter++) 119 { 120 cout << flag++ << ": " << *iter << endl; 121 outfile << *iter << endl; 122 } 123 outfile.close(); 124 } 125 } 126 127 clock_t end_time=clock(); //结束时间 128 129 cout<< "Running time is: " 130 <<static_cast<double>(end_time - start_time)/CLOCKS_PER_SEC 131 <<"(s)"<<endl; //输出运行时间 132 return 0; 133 }
posted on 2017-05-13 16:19 HelloShijam 阅读(523) 评论(0) 编辑 收藏 举报