2018年6月30日土曜日

開発環境

Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセスとシステムサービス - 限界を超える)、コードマグネット(p. 399)を取り組んでみる。

コードマグネット(p. 399)

Makefile

CC = cc

all: sample run

sample: sample.c
 $(CC) sample.c -o sample

run: sample
 ./sample

コード

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char *now() {
  time_t t;
  time(&t);

  return asctime(localtime(&t));
}

int main() {
  char comment[80];
  char cmd[120];

  fgets(comment, 80, stdin);
  sprintf(cmd, "echo '%s %s' >> reports.log", comment, now());
  system(cmd);
}

入出力結果(Terminal)

$ make
cc sample.c -o sample
./sample
comment1
$ cat reports.log 
comment1
 Sat Jun 30 13:25:05 2018

$ make
./sample
comment2
$ cat reports.log 
comment1
 Sat Jun 30 13:25:05 2018

comment2
 Sat Jun 30 13:25:12 2018

$ 

0 コメント:

コメントを投稿