1. 程式人生 > >從資料夾讀取檔案

從資料夾讀取檔案

// test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<vld.h>
#include<string>
#include<stdio.h>  
#include<algorithm>  
#include<vector>  
#include<iostream>  
#include<stdlib.h>
#include<typeinfo>
#include<utility>
#include<tuple> #include<fstream> #include<bitset> #include<io.h> using namespace std; void getfile(string filepath, vector<string> &files) { struct _finddata_t fileinfo; auto handle=_findfirst(string(filepath).append("\\*").c_str(), &fileinfo); if
(handle == -1) { perror("file not exist"); exit(1); } /*if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..")!=0) files.push_back(string(filepath).append("\\").append(fileinfo.name)); cout << fileinfo.name << endl;*/ //用do...while可以把第一次統一進來,就不用像上面一樣單獨處理了
do { cout << fileinfo.name << endl; if (fileinfo.attrib & _A_SUBDIR) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) getfile(string(filepath).append("\\" + string(fileinfo.name)), files); } else files.push_back(string(filepath).append("\\").append(fileinfo.name)); } while (_findnext(handle, &fileinfo) == 0); _findclose(handle); } int main() { string filepath = "F:\\aaa"; vector<string> files; getfile(filepath, files); for (decltype(files.size()) i = 0; i != files.size(); ++i) { cout << files[i] << endl; ofstream os(files[i]); if (!os) { perror("file not open"); exit(1); } os << to_string(i).append("*************").append(to_string(i))<< endl; } return 0; }