2018年4月24日火曜日

開発環境

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

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

Makefile

CC = cc

all: 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
 Tue Apr 24 14:52:45 2018

$ make
./sample
comment2
$ cat reports.log 
comment1
 Tue Apr 24 14:52:45 2018

comment2
 Tue Apr 24 14:52:50 2018

$ 

0 コメント:

コメントを投稿