基本的思想是,先檢查當前目錄下有多少種類型的源文件,好比.cpp,.c,.cc,.java等都屬於不一樣類型的源文件,而後採用_finddata_t結構體表徵不一樣類型的文件,經過_findfirst和_findnext函數一一進行匹配,直到所有找到爲止。java
再分別打開這些文件,並打開同名的文件加上.txt後綴,將源文件讀入到文本文件當中,至此完成。ios
main.cppwindows
#include "stdafx.h" #include "FileTran.h" #include "ListFile.h" #include<iostream> #include<windows.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { try{ FileTran fileTran; ListFile listFile; listFile.listFiles(); printf("listfile\n"); unsigned int num=listFile.getNum(); printf("num=%d\n",num); unsigned int i=0; while(i<num){ cout<<listFile.getFile(i)<<endl; if(fileTran.openFile(listFile.getFile(i))){ fileTran.handleFile(); i++; if(i==8)break; } } } catch(...){ printf("something is wrong!\n"); } std::cout<<"--------done"<<std::endl; Sleep(2000); return 0; }
FileTran.cpp 函數
#include "StdAfx.h" #include "FileTran.h" #include <cstdio> #include <string> #include<iostream> using std::ios_base; using std::string; using std::getline; FileTran::FileTran(void) { } FileTran::~FileTran(void) { close(); } bool FileTran::openFile(const char* filename) { finFile.open(filename,ios_base::in|ios_base::binary); if(!finFile.is_open()){ printf("can not open the file,please check it out!\n"); throw "open error"; } else{ this->filename=filename; printf("File opened\n"); return true; } } void FileTran::close() { } void FileTran::handleFile() { string tmpname=string(filename)+string(".txt"); if(finFile.is_open()){ foutFile.open(tmpname.c_str(),ios_base::out|ios_base::binary); if(!foutFile.is_open()){ throw "something is wrong"; } } string str; while(!finFile.eof()){ std::getline(finFile,str); foutFile<<str<<std::endl; } finFile.close(); foutFile.close(); }
FileTran.hthis
#ifndef _FILETRAN_ #define _FILETRAN_ #include<fstream> using std::ifstream; using std::ofstream; class FileTran { public: FileTran(void); ~FileTran(void); public: bool openFile(const char*); void handleFile(); private: void close(); private: const char* filename; ifstream finFile; ofstream foutFile; }; #endif //file tran
ListFile.cpp spa
#include "StdAfx.h" #include "ListFile.h" #include<iostream> #include<fstream> #include<io.h> #include<windows.h> #include<tchar.h> ListFile::ListFile(void) { } ListFile::~ListFile(void) { } unsigned int ListFile::listFiles() { unsigned int cnt=0; char buf[SIZEFILE*2]; printf("Please enter all sorts of files you wanna transfer:"); while(EOF==scanf("%d",&kindsFiles)){ printf("enter is wrong!\n"); } printf("Please enter the regulations of files,for example: \"*.c\" means all .c files would be listed!\n"); while(EOF!=scanf("%s",regs[cnt++])){ if(cnt==kindsFiles) break; } cnt=0; while(cnt!=kindsFiles){ struct _finddata_t files; long handle=_findfirst(regs[cnt++],&files); if(-1==handle){ printf("can not match the files!\n"); while(true); exit(-1); } string tmp=string(files.name); filesList.push_back(tmp); while(!_findnext(handle,&files)){ tmp=string(files.name); filesList.push_back(tmp); } } nums=filesList.size(); return nums; } void ListFile::disFiles() { list<string>::iterator listPtr=filesList.begin(); for(int i=0;i<nums;i++,listPtr++){ strcpy(allFiles[i],listPtr->c_str()); } } char* ListFile::getFile(unsigned int n) { disFiles(); return allFiles[n]; } unsigned int ListFile::getNum() { return nums; }
ListFile.h code
#ifndef _LISTFILE_ #define _LISTFILE_ #include<cstdio> #include<string> #include<list> #define SIZEFILE 80 #define NUMFILE 80 using std::string; using std::list; class ListFile { public: ListFile(void); ~ListFile(void); private: unsigned int kindsFiles; char regs[NUMFILE][SIZEFILE]; list<string> filesList; char allFiles[NUMFILE][SIZEFILE]; unsigned int nums; public: unsigned int listFiles(); void disFiles(); char* getFile(unsigned int); unsigned int getNum(); }; #endif //list file
stdafx.cpp get
#include "stdafx.h"
stdafx.hstring
#pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h>
targetver.hit
#pragma once // 包括 SDKDDKVer.h 將定義可用的最高版本的 Windows 平臺。 // 若是要爲之前的 Windows 平臺生成應用程序,請包括 WinSDKVer.h,並將 // WIN32_WINNT 宏設置爲要支持的平臺,而後再包括 SDKDDKVer.h。 #include <SDKDDKVer.h>
在visual studio 2010版及以上版本上編譯,可生成.exe文件,將這個.exe文件和須要轉換的源代碼文件放到同一個目錄中,打開.exe文件按照提示進行操做便可!