teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

実行結果の修正

2018/01/11 08:54

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -5,92 +5,84 @@
5
5
  コンパイルの仕方が間違っているのでしょうか。助言、ご教授をお願いします。
6
6
  ```ここに言語を入力
7
7
  コード
8
- read_line.c
8
+ read_file.c
9
+ // 標準入力からテキストファイルを読み込み,
10
+ // それを標準出力に吐くプログラムです。
9
11
  #include <stdio.h>
10
12
  #include <stdlib.h>
11
- #include <string.h>
12
13
  #include <assert.h>
13
14
 
14
- #define ALLOC_SIZE
15
+ #define ALLOC_SIZE (256)
15
- static char *st_line_buffer=NULL;
16
- static int st_current_buffer_size=0;
17
- static int st_current_used_size=0;
18
16
 
17
+ #include "read_line.h"
18
+
19
+ char **add_line(char **text_data, char *line,
19
- static void add_character(int ch)
20
+ int *line_alloc_num, int *line_num)
20
21
  {
21
- assert(st_current_buffer_size >= st_current_used_size;
22
+ assert(*line_alloc_num >= *line_num);
23
+ if (*line_alloc_num == *line_num) {
24
+ text_data = realloc(text_data,
25
+ (*line_alloc_num + ALLOC_SIZE) * sizeof(char*));
26
+ *line_alloc_num += ALLOC_SIZE;
27
+ }
28
+ text_data[*line_num] = line;
29
+ (*line_num)++;
22
30
 
23
- if(st_current_buffer_size == st_current_used_size{
24
- st_line_buffer=realloc(st_line_buffer,
25
- (st_current_buffer_size+ALLC_SIZE)
26
- *sizeof(char));
27
- st_current_buffer_size += ALLC_SIZE;
28
- }
29
-
30
- // バッファの末尾に1文字追加。
31
- st_line_buffer[st_current_used_size]=ch;
32
- st_current_used_size++;
31
+ return text_data;
33
32
  }
34
33
 
35
- //fpから1行読み込む。ファイルの末尾に来たら,NULLを返す。
36
- char *read_line(FILE *fp)
34
+ char **read_file(FILE *fp, int *line_num_p)
37
35
  {
36
+ char **text_data = NULL;
38
- int ch;
37
+ int line_num = 0;
39
-
38
+ int line_alloc_num = 0;
40
- char *ret;
39
+ char *line;
41
40
 
42
- st_current_used_size=0;
43
-
44
- while((ch=getc(fp)) != EOF{
41
+ while ((line = read_line(fp)) != NULL) {
45
- if(ch=='\n'){
42
+ text_data = add_line(text_data, line,
46
- add_character('\0');
43
+ &line_alloc_num, &line_num);
47
- break;
48
- }
44
+ }
49
- add_character(ch);
50
- }
51
- if(ch==EOF){
52
- if(st_current_used_size>0){
53
- add_character('\0');
54
- // 最後の行の後に改行がなかった場合
55
- }else{
56
- return NULL;
57
- // ファイルが終わったら、NULL返す。
45
+ /* text_data、本当に必要なサイズまで縮める */
58
- }
59
- }
60
- ret=malloc(sizeof(char)*st_current_used_size);
46
+ text_data = realloc(text_data, line_num * sizeof(char*));
61
- strcpy(ret,st_line_buffer);
47
+ *line_num_p = line_num;
62
-
48
+
63
- return ret;
49
+ return text_data;
64
50
  }
65
51
 
66
- void free_buffer(void)
52
+ int main(void)
67
53
  {
54
+ char **text_data;
68
- free(st_line_buffer);
55
+ int line_num;
69
- st_line_buffer=NULL;
56
+ int i;
70
- st_current_buffer_size=0;
71
- st_current_used_size=0;
72
57
 
58
+ text_data = read_file(stdin, &line_num);
59
+
60
+ for (i = 0; i < line_num; i++) {
61
+ printf("%s\n", text_data[i]);
62
+ }
63
+ free_buffer();
64
+
65
+ return 0;
73
66
  }
74
67
 
75
68
  read_line.h
76
69
  #ifndef READ_LINE_H_INCLUDED
77
70
  #define READ_LINE_H_INCLUDED
78
71
 
79
- include <stdio.h>
72
+ #include <stdio.h>
80
73
 
81
74
  extern char *read_line(FILE *fp);
82
75
  void free_buffer(void);
83
76
 
84
- #endif /* READ_LINE_H_INCLUDED */
77
+ #endif
85
78
 
86
- read_file.c
79
+ read_line.c
87
80
  // 標準入力からテキストファイルを読み込み,
88
81
  // それを標準出力に吐くプログラムです。
89
-
90
82
  #include <stdio.h>
91
83
  #include <stdlib.h>
92
84
  #include <assert.h>
93
-
85
+ #include <string.h>
94
86
  #define ALLOC_SIZE (256)
95
87
 
96
88
  #include "read_line.h"
@@ -146,20 +138,23 @@
146
138
 
147
139
  実行結果
148
140
  naka@naka ~/src_dos
149
- $ gcc -c read_line.c
141
+ $ gcc -c read_line.h
150
- gcc.exe: error: read_line.c: No such file or directory
151
- gcc.exe: fatal error: no input files
152
- compilation terminated.
153
142
 
154
143
  naka@naka ~/src_dos
155
144
  $ gcc -c read_file.c
156
- read_file.c:7:23: fatal error: read_line.h: No such file or directory
157
- #include "read_line.h"
158
- ^
159
- compilation terminated.
160
145
 
161
146
  naka@naka ~/src_dos
162
- $ gcc -o read_file read_file.c read_line.c -Wall
147
+ $ gcc -c read_line.c
148
+ $ gcc -c read_line.c
149
+ read_line.c: In function 'add_character':
163
- gcc.exe: error: read_line1.c: No such file or directory
150
+ read_line.c:33:39: error: expected expression before ')' token
151
+ (st_current_buffer_size+ALLOC_SIZE)*sizeof(char));
152
+ ^
153
+ read_line.c:34:39: error: expected expression before ';' token
154
+ st_current_buffer_size += ALLOC_SIZE;
155
+ ^
164
156
 
157
+ naka@naka ~/src_dos
158
+ $
159
+
165
160
  ```