開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の18章(モジュールプログラミング)、18-14(プログラミング実習)、実習18-1.を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
18-14(プログラミング実習)、実習18-1
コード
temp.h
extern void open_file(char *name); extern void define_header(char *heading); extern void print_line(char *line); extern void page(void); extern void close_file(void);
コード
temp.c
#include <stdio.h>
#include <stdlib.h>
static FILE *out_file;
void open_file(char *name)
{
out_file = fopen(name, "w");
if(out_file == NULL){
fprintf(stderr, "Error: Unable to output file '%s'\n", name);
exit (8);
}
}
void define_header(char *heading)
{
fprintf(out_file, "HEAD: %s\n", heading);
}
void print_line(char *line)
{
fprintf(out_file, "%s", line);
}
void page(void)
{
fputc('\f', out_file);
}
void close_file(void)
{
fclose(out_file);
}
コード
sample.c
#include "temp.h"
#include <stdio.h>
#include <stdlib.h>
static int lineno = 1;
int main(int argc, char *argv[])
{
FILE *in_file;
char line[100];
char *str_ptr;
if(argc != 3){
fprintf(stderr, "Error");
exit (8);
}
argv++;
in_file = fopen(*argv, "r");
if(in_file == NULL){
fprintf(stderr, "Error: Unable to open input file '%s'\n", *argv);
exit (8);
}
argv++;
open_file(*argv);
define_header("OUTPUT FILE");
lineno++;
while (1){
str_ptr = fgets(line, sizeof(line), in_file);
if(str_ptr == NULL){
break;
}
if(lineno > 10){
page();
lineno = 1;
}
print_line(line);
lineno++;
}
fclose(in_file);
close_file();
return (0);
}
Makefile
CC=cc CFLAGS=-g OBJS=sample.o temp.o all: sample sample: $(OBJS) $(CC) $(CFLAGS) -o sample $(OBJS) sample.o: temp.h sample.c temp.o: temp.c clean: rm -f sample sample.o temp.o
入出力結果(Terminal)
$ make
cc -g -c -o sample.o sample.c
cc -g -c -o temp.o temp.c
cc -g -o sample sample.o temp.o
$ ./sample sample.c sample.c.txt
$ ./sample temp.c temp.c.txt
$ ./sample temp.h temp.h.txt
$ cat -nv sample.c.txt temp.c.txt temp.h.txt
1 HEAD: OUTPUT FILE
2 #include "temp.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 static int lineno = 1;
7
8 int main(int argc, char *argv[])
9 {
10 FILE *in_file;
11 ^L char line[100];
12 char *str_ptr;
13
14 if(argc != 3){
15 fprintf(stderr, "Error");
16 exit (8);
17 }
18 argv++;
19 in_file = fopen(*argv, "r");
20 if(in_file == NULL){
21 ^L fprintf(stderr, "Error: Unable to open input file '%s'\n", *argv);
22 exit (8);
23 }
24 argv++;
25 open_file(*argv);
26 define_header("OUTPUT FILE");
27 lineno++;
28 while (1){
29 str_ptr = fgets(line, sizeof(line), in_file);
30 if(str_ptr == NULL){
31 ^L break;
32 }
33 if(lineno > 10){
34 page();
35 lineno = 1;
36 }
37 print_line(line);
38 lineno++;
39 }
40 fclose(in_file);
41 ^L close_file();
42 return (0);
43 }
1 HEAD: OUTPUT FILE
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 static FILE *out_file;
6
7 void open_file(char *name)
8 {
9 out_file = fopen(name, "w");
10 if(out_file == NULL){
11 ^L fprintf(stderr, "Error: Unable to output file '%s'\n", name);
12 exit (8);
13 }
14 }
15
16 void define_header(char *heading)
17 {
18 fprintf(out_file, "HEAD: %s\n", heading);
19 }
20
21 ^Lvoid print_line(char *line)
22 {
23 fprintf(out_file, "%s", line);
24 }
25
26 void page(void)
27 {
28 fputc('\f', out_file);
29 }
30
31 ^Lvoid close_file(void)
32 {
33 fclose(out_file);
34 }
1 HEAD: OUTPUT FILE
2 extern void open_file(char *name);
3 extern void define_header(char *heading);
4 extern void print_line(char *line);
5 extern void page(void);
6 extern void close_file(void);
$
0 コメント:
コメントを投稿