3

システムの日付と時刻をファイルのファイル名として使用する出席システムを作成したかったのは、たとえば、これが通常の方法です。

int main () {
time_t t = time(0);   // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-'
     << (now->tm_mon + 1) << '-'
     <<  now->tm_mday
     << endl;
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
} 

しかし、example.txtの代わりにシステムの日付と時刻が必要です。上記のプログラムにctimeヘッダーファイルを含めることで時間を計算しましたが、これは単なる例です。

4

4 に答える 4

0

ostringstream を使用して日付文字列を作成し (cout で行っているように)、そのstr()メンバー関数を使用して対応する日付文字列を取得できます。

于 2014-03-11T06:55:02.787 に答える
0

この目的で stringstream クラスを使用できます。次に例を示します。

int main (int argc, char *argv[])
{
  time_t t = time(0);   // get time now
  struct tm * now = localtime( & t );
  stringstream ss;

  ss << (now->tm_year + 1900) << '-'
     << (now->tm_mon + 1) << '-'
     <<  now->tm_mday
     << endl;

  ofstream myfile;
  myfile.open (ss.str());
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;

  return(0);
}
于 2014-03-11T06:55:45.757 に答える