ログイン
編集不可のページディスカッション情報添付ファイル
"solanumaple/makecpp"の差分

MMA
2と3のリビジョン間の差分
2014-07-17 13:35:46時点のリビジョン2
サイズ: 285
編集者: solanumaple
コメント:
2014-07-17 13:43:38時点のリビジョン3
サイズ: 3056
編集者: solanumaple
コメント:
削除された箇所はこのように表示されます。 追加された箇所はこのように表示されます。
行 5: 行 5:
ディレクトリ内に a.cpp~z.cppが存在しなければそのファイル名で ディレクトリ内に a.cpp~z.cppが存在しなければそのファイル名で,
全て存在すればその時の時刻をファイル名にして出力します

※<dirent.h>が環境依存 自分はsolで使っている


{{{#!highlight c++
#include<iostream>
#include<fstream>
#include<ctime>
#include<sstream>
#include<string>
#include<vector>
#include<unistd.h>
#include<dirent.h>

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<string> used;
  vector<string>::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 << 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<iostream>" << endl;
    ofs << "//#include<iomanip>" << endl;

    ofs << "//#include<cstdlib>" << endl;
    ofs << "//#include<cstdio>" << endl;
    ofs << "//#include<cmath>" << endl;
    ofs << "//#include<cstring>" << endl;

    ofs << "//#include<vector>" << endl;
    ofs << "//#include<algorithm>" << endl;
    ofs << "//#include<string>" << endl;
    ofs << "//#include<sstream>" << endl;
    ofs << "//#include<ncurses.h>" << 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;
  }

}

}}}

学習がてら初めて使えるものを作ったので自慢 "改善"してご自由にお使いください このコードを使用したことによる一切の責任は持てません。

ディレクトリ内に a.cpp~z.cppが存在しなければそのファイル名で, 全て存在すればその時の時刻をファイル名にして出力します

※<dirent.h>が環境依存 自分はsolで使っている

   1 #include<iostream>
   2 #include<fstream>
   3 #include<ctime>
   4 #include<sstream>
   5 #include<string>
   6 #include<vector>
   7 #include<unistd.h>
   8 #include<dirent.h>
   9 
  10 using namespace std;
  11 void out(const char*);
  12 string name(void);
  13 
  14 int main(){
  15   out( name().c_str() );
  16   return 0;
  17 }
  18 
  19 string name(){
  20   // a.cpp^z.cppまで探して全て存在していたら現在時刻.cpp のパスを返す
  21   char path[2000];//パス名全体の長さの上限1023バイト
  22   stringstream ss;
  23   getcwd(path,1030);
  24   ss.str("");
  25   ss.clear(stringstream::goodbit);
  26   ss << path << '/';
  27 
  28   //a-zまで探す
  29   struct dirent* dirst;
  30   DIR* dir = opendir(path);
  31   vector<string> used;
  32   vector<string>::iterator itnm;
  33   stringstream tmp;
  34   bool safe=false;//ディレクトリ読めたかフラグ
  35   bool nexist;//存在するとき0,しないとき1
  36 
  37   while( (dirst = readdir(dir)) != NULL ){
  38     used.push_back( dirst->d_name );
  39     safe = true;
  40   }
  41   closedir(dir);
  42 
  43   for(char i='a'; i<='z'; i++){
  44     nexist = 1;
  45     tmp.clear(stringstream::goodbit); tmp.str("");
  46     tmp << i << ".cpp";
  47     for( itnm = used.begin(); itnm != used.end(); itnm++){
  48       if( *itnm != tmp.str() )
  49         nexist *= 1;
  50       else
  51         nexist *= 0;
  52     }
  53     if( nexist && safe){//ファイルが存在しないとき
  54       ss << tmp.str();
  55       return ss.str();
  56     }
  57   }
  58   //a-zまで探す
  59 
  60   //しかたないから時刻をファイル名に
  61   time_t timer = time(NULL);
  62   struct tm* str = localtime( &timer );
  63   ss << str->tm_year+1900 << str->tm_mon+1 << str->tm_mday << str->tm_hour << str->tm_min << str->tm_sec << ".cpp";
  64   return ss.str();
  65   //時刻
  66 }
  67 
  68 void out(const char* filename){
  69 
  70   ofstream ofs( filename, ios::app);
  71   if(ofs){
  72     cout << filename << "を生成" << endl;
  73     ofs << "#include<iostream>" << endl;
  74     ofs << "//#include<iomanip>" << endl;
  75 
  76     ofs << "//#include<cstdlib>" << endl;
  77     ofs << "//#include<cstdio>" << endl;
  78     ofs << "//#include<cmath>" << endl;
  79     ofs << "//#include<cstring>" << endl;
  80 
  81     ofs << "//#include<vector>" << endl;
  82     ofs << "//#include<algorithm>" << endl;
  83     ofs << "//#include<string>" << endl;
  84     ofs << "//#include<sstream>" << endl;
  85     ofs << "//#include<ncurses.h>" << endl << endl;
  86     ofs << "using namespace std;" << endl << endl;
  87     ofs << "int main(){" << endl;
  88     ofs << "  ios::sync_with_stdio(false);" << endl << endl;
  89     ofs << "  return 0;" << endl;
  90     ofs << "}" << endl;
  91   }else{
  92     cerr << "出力ファイルを開けませんでした" << endl;
  93   }
  94 
  95 }

solanumaple/makecpp (最終更新日時 2014-07-17 13:55:28 更新者 solanumaple)