開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio 2017 (、Clang/C2(試験的))
- プログラミング言語: C(Visual C)
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセスとシステムサービス - 限界を超える)、コードマグネット(p. 399)を取り組んでみる。
コードマグネット(p. 399)
コード
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
char *now()
{
struct tm newtime;
char buffer[32];
__time32_t aclock;
_time32(&aclock);
_localtime32_s(&newtime, &aclock);
asctime_s(buffer, 32, &newtime);
return _strdup(buffer);
}
int main()
{
char comment[80];
char cmd[120];
char *t = now();
fgets(comment, 80, stdin);
comment[strlen(comment) - 1] = '\0';
t[strlen(t) - 1] = '\0';
sprintf_s(cmd, 120, "echo '%s %s' >> reports.log", comment, t);
free(t);
puts(cmd);
system(cmd);
return 0;
}
入出力結果(コマンドプロンプト)
>timestamp.exe コメント echo 'コメント Sat Dec 30 11:34:29 2017' >> reports.log >type reports.log 'コメント Sat Dec 30 11:34:29 2017' >
0 コメント:
コメントを投稿