2014年5月9日金曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の3章(小さなツールの作成 - 1つのことだけをうまくやる)、自分兌換が得てみよう(p.139)を解いてみる。

その他参考書籍

自分兌換が得てみよう(p.139)

コード(BBEdit, Emacs)

sample139.c

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

int main(int argc, char *argv[])
{
  char line[80];
  FILE *in_file = fopen("spooky.csv", "r");
  FILE *out_file1 = fopen("ufos.csv", "w");
  FILE *out_file2 = fopen("disappearances.csv", "w");
  FILE *out_file3 = fopen("others.csv", "w");

  while (fscanf(in_file, "%79[^\n]\n", line) == 1)
    if (strstr(line, "UFO"))
      fprintf(out_file1, "%s\n", line);
    else if (strstr(line, "Disappearance"))
      fprintf(out_file2, "%s\n", line);
    else
      fprintf(out_file3, "%s\n", line);
  
  fclose(in_file);
  fclose(out_file1);
  fclose(out_file2);
  fclose(out_file3);

  return (0);
}

Makefile

CC=cc
CFLAGS = -g -Wall
SRC=sample139.c

all: sample139

sample139: sample139.c
 $(CC) $(CFLAGS) -o sample139 sample139.c

clean:
 rm -rf sample139

入出力結果(Terminal)

$ cat spooky.csv 
30.685163,-68.137207,Type=Yeti
28.304380,-74.575195,Type=UFO
29.132971,-71.136475,Type=Ship
28.343065,-62.753906,Type=Elvis
27.868217,-68.005371,Type=Goatsucker
30.496017,-73.333740,Type=Disappearance
26.224447,-71.477051,Type=UFO
29.401320,-66.027832,Type=Ship
37.879536,-69.477539,Type=Elvis
22.705256,-68.192139,Type=Elvis
27.166695,-87.484131,Type=Elvis
$ make && ./sample139
cc -g -Wall -o sample139 sample139.c
$ cat ufos.csv 
28.304380,-74.575195,Type=UFO
26.224447,-71.477051,Type=UFO
$ cat disappearances.csv 
30.496017,-73.333740,Type=Disappearance
$ cat others.csv 
30.685163,-68.137207,Type=Yeti
29.132971,-71.136475,Type=Ship
28.343065,-62.753906,Type=Elvis
27.868217,-68.005371,Type=Goatsucker
29.401320,-66.027832,Type=Ship
37.879536,-69.477539,Type=Elvis
22.705256,-68.192139,Type=Elvis
27.166695,-87.484131,Type=Elvis
$ 

0 コメント:

コメントを投稿