質問編集履歴

1

実行結果の修正

2018/01/11 08:54

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -12,178 +12,162 @@
12
12
 
13
13
  コード
14
14
 
15
+ read_file.c
16
+
17
+ // 標準入力からテキストファイルを読み込み,
18
+
19
+ // それを標準出力に吐くプログラムです。
20
+
21
+ #include <stdio.h>
22
+
23
+ #include <stdlib.h>
24
+
25
+ #include <assert.h>
26
+
27
+
28
+
29
+ #define ALLOC_SIZE (256)
30
+
31
+
32
+
33
+ #include "read_line.h"
34
+
35
+
36
+
37
+ char **add_line(char **text_data, char *line,
38
+
39
+ int *line_alloc_num, int *line_num)
40
+
41
+ {
42
+
43
+ assert(*line_alloc_num >= *line_num);
44
+
45
+ if (*line_alloc_num == *line_num) {
46
+
47
+ text_data = realloc(text_data,
48
+
49
+ (*line_alloc_num + ALLOC_SIZE) * sizeof(char*));
50
+
51
+ *line_alloc_num += ALLOC_SIZE;
52
+
53
+ }
54
+
55
+ text_data[*line_num] = line;
56
+
57
+ (*line_num)++;
58
+
59
+
60
+
61
+ return text_data;
62
+
63
+ }
64
+
65
+
66
+
67
+ char **read_file(FILE *fp, int *line_num_p)
68
+
69
+ {
70
+
71
+ char **text_data = NULL;
72
+
73
+ int line_num = 0;
74
+
75
+ int line_alloc_num = 0;
76
+
77
+ char *line;
78
+
79
+
80
+
81
+ while ((line = read_line(fp)) != NULL) {
82
+
83
+ text_data = add_line(text_data, line,
84
+
85
+ &line_alloc_num, &line_num);
86
+
87
+ }
88
+
89
+ /* text_dataを、本当に必要なサイズまで縮める */
90
+
91
+ text_data = realloc(text_data, line_num * sizeof(char*));
92
+
93
+ *line_num_p = line_num;
94
+
95
+
96
+
97
+ return text_data;
98
+
99
+ }
100
+
101
+
102
+
103
+ int main(void)
104
+
105
+ {
106
+
107
+ char **text_data;
108
+
109
+ int line_num;
110
+
111
+ int i;
112
+
113
+
114
+
115
+ text_data = read_file(stdin, &line_num);
116
+
117
+
118
+
119
+ for (i = 0; i < line_num; i++) {
120
+
121
+ printf("%s\n", text_data[i]);
122
+
123
+ }
124
+
125
+ free_buffer();
126
+
127
+
128
+
129
+ return 0;
130
+
131
+ }
132
+
133
+
134
+
135
+ read_line.h
136
+
137
+ #ifndef READ_LINE_H_INCLUDED
138
+
139
+ #define READ_LINE_H_INCLUDED
140
+
141
+
142
+
143
+ #include <stdio.h>
144
+
145
+
146
+
147
+ extern char *read_line(FILE *fp);
148
+
149
+ void free_buffer(void);
150
+
151
+
152
+
153
+ #endif
154
+
155
+
156
+
15
157
  read_line.c
16
158
 
159
+ // 標準入力からテキストファイルを読み込み,
160
+
161
+ // それを標準出力に吐くプログラムです。
162
+
17
163
  #include <stdio.h>
18
164
 
19
165
  #include <stdlib.h>
20
166
 
167
+ #include <assert.h>
168
+
21
169
  #include <string.h>
22
170
 
23
- #include <assert.h>
24
-
25
-
26
-
27
- #define ALLOC_SIZE
28
-
29
- static char *st_line_buffer=NULL;
30
-
31
- static int st_current_buffer_size=0;
32
-
33
- static int st_current_used_size=0;
34
-
35
-
36
-
37
- static void add_character(int ch)
38
-
39
- {
40
-
41
- assert(st_current_buffer_size >= st_current_used_size;
42
-
43
-
44
-
45
- if(st_current_buffer_size == st_current_used_size{
46
-
47
- st_line_buffer=realloc(st_line_buffer,
48
-
49
- (st_current_buffer_size+ALLC_SIZE)
50
-
51
- *sizeof(char));
52
-
53
- st_current_buffer_size += ALLC_SIZE;
54
-
55
- }
56
-
57
-
58
-
59
- // バッファの末尾に1文字追加。
60
-
61
- st_line_buffer[st_current_used_size]=ch;
62
-
63
- st_current_used_size++;
64
-
65
- }
66
-
67
-
68
-
69
- //fpから1行読み込む。ファイルの末尾に来たら,NULLを返す。
70
-
71
- char *read_line(FILE *fp)
72
-
73
- {
74
-
75
- int ch;
76
-
77
-
78
-
79
- char *ret;
80
-
81
-
82
-
83
- st_current_used_size=0;
84
-
85
-
86
-
87
- while((ch=getc(fp)) != EOF{
88
-
89
- if(ch=='\n'){
90
-
91
- add_character('\0');
92
-
93
- break;
94
-
95
- }
96
-
97
- add_character(ch);
98
-
99
- }
100
-
101
- if(ch==EOF){
102
-
103
- if(st_current_used_size>0){
104
-
105
- add_character('\0');
106
-
107
- // 最後の行の後に改行がなかった場合
108
-
109
- }else{
110
-
111
- return NULL;
112
-
113
- // ファイルが終わったら、NULLを返す。
114
-
115
- }
116
-
117
- }
118
-
119
- ret=malloc(sizeof(char)*st_current_used_size);
120
-
121
- strcpy(ret,st_line_buffer);
122
-
123
-
124
-
125
- return ret;
126
-
127
- }
128
-
129
-
130
-
131
- void free_buffer(void)
132
-
133
- {
134
-
135
- free(st_line_buffer);
136
-
137
- st_line_buffer=NULL;
138
-
139
- st_current_buffer_size=0;
140
-
141
- st_current_used_size=0;
142
-
143
-
144
-
145
- }
146
-
147
-
148
-
149
- read_line.h
150
-
151
- #ifndef READ_LINE_H_INCLUDED
152
-
153
- #define READ_LINE_H_INCLUDED
154
-
155
-
156
-
157
- include <stdio.h>
158
-
159
-
160
-
161
- extern char *read_line(FILE *fp);
162
-
163
- void free_buffer(void);
164
-
165
-
166
-
167
- #endif /* READ_LINE_H_INCLUDED */
168
-
169
-
170
-
171
- read_file.c
172
-
173
- // 標準入力からテキストファイルを読み込み,
174
-
175
- // それを標準出力に吐くプログラムです。
176
-
177
-
178
-
179
- #include <stdio.h>
180
-
181
- #include <stdlib.h>
182
-
183
- #include <assert.h>
184
-
185
-
186
-
187
171
  #define ALLOC_SIZE (256)
188
172
 
189
173
 
@@ -294,35 +278,41 @@
294
278
 
295
279
  naka@naka ~/src_dos
296
280
 
281
+ $ gcc -c read_line.h
282
+
283
+
284
+
285
+ naka@naka ~/src_dos
286
+
287
+ $ gcc -c read_file.c
288
+
289
+
290
+
291
+ naka@naka ~/src_dos
292
+
297
293
  $ gcc -c read_line.c
298
294
 
295
+ $ gcc -c read_line.c
296
+
299
- gcc.exe: error: read_line.c: No such file or directory
297
+ read_line.c: In function 'add_character':
298
+
300
-
299
+ read_line.c:33:39: error: expected expression before ')' token
300
+
301
+ (st_current_buffer_size+ALLOC_SIZE)*sizeof(char));
302
+
303
+ ^
304
+
305
+ read_line.c:34:39: error: expected expression before ';' token
306
+
301
- gcc.exe: fatal error: no input files
307
+ st_current_buffer_size += ALLOC_SIZE;
302
-
308
+
303
- compilation terminated.
309
+ ^
304
310
 
305
311
 
306
312
 
307
313
  naka@naka ~/src_dos
308
314
 
309
- $ gcc -c read_file.c
310
-
311
- read_file.c:7:23: fatal error: read_line.h: No such file or directory
312
-
313
- #include "read_line.h"
314
-
315
- ^
315
+ $
316
-
317
- compilation terminated.
318
-
319
-
320
-
321
- naka@naka ~/src_dos
322
-
323
- $ gcc -o read_file read_file.c read_line.c -Wall
324
-
325
- gcc.exe: error: read_line1.c: No such file or directory
326
316
 
327
317
 
328
318