学習がてら,初めて使えるものを作れたので自慢<
> "改善"してご自由にお使いください<
> このコードを使用したことによる一切の責任は持てません。<
> ディレクトリ内に a.cpp~z.cppが存在しなければそのファイル名で,<
> 全て存在すればその時の時刻をファイル名にして以下を出力します<
> {{{#!highlight c++ #include //#include //#include //#include //#include //#include //#include //#include //#include //#include //#include using namespace std; int main(){ ios::sync_with_stdio(false); return 0; } }}} {{{#!highlight c++ #include #include #include #include #include #include #include #include using namespace std; void out(const char*); string name(void); int main(){ out( name().c_str() ); return 0; } string name(){ // a.cpp^z.cppまで探して全て存在していたら現在時刻.cpp のパスを返す char path[2000];//パス名全体の長さの上限1023バイト stringstream ss; getcwd(path,1030); ss.str(""); ss.clear(stringstream::goodbit); ss << path << '/'; //a-zまで探す struct dirent* dirst; DIR* dir = opendir(path); vector used; vector::iterator itnm; stringstream tmp; bool safe=false;//ディレクトリ読めたかフラグ bool nexist;//存在するとき0,しないとき1 while( (dirst = readdir(dir)) != NULL ){ used.push_back( dirst->d_name ); safe = true; } closedir(dir); for(char i='a'; i<='z'; i++){ nexist = 1; tmp.clear(stringstream::goodbit); tmp.str(""); tmp << i << ".cpp"; for( itnm = used.begin(); itnm != used.end(); itnm++){ if( *itnm != tmp.str() ) nexist *= 1; else nexist *= 0; } if( nexist && safe){//ファイルが存在しないとき ss << tmp.str(); return ss.str(); } } //a-zまで探す //しかたないから時刻をファイル名に time_t timer = time(NULL); struct tm* str = localtime( &timer ); ss << str->tm_year+1900 << str->tm_mon+1 << str->tm_mday; ss << str->tm_hour << str->tm_min << str->tm_sec << ".cpp"; return ss.str(); //時刻 } void out(const char* filename){ ofstream ofs( filename, ios::app); if(ofs){ cout << filename << "を生成" << endl; ofs << "#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl; ofs << "//#include" << endl << endl; ofs << "using namespace std;" << endl << endl; ofs << "int main(){" << endl; ofs << " ios::sync_with_stdio(false);" << endl << endl; ofs << " return 0;" << endl; ofs << "}" << endl; }else{ cerr << "出力ファイルを開けませんでした" << endl; } } }}} ※一部ヘッダが環境依存 自分はsolで使っている<
>