質問編集履歴
6
"LACKNUM\.DAT"の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -144,7 +144,47 @@
|
|
144
144
|
|
145
145
|
これをバイナリデータに変換したつもりの"LACKNUM.DAT"を使って実行しています。
|
146
146
|
|
147
|
-
|
147
|
+
32 30 31 37 20 20 39 20 31 36 20 20 38 20 20 31
|
148
|
+
|
149
|
+
38 20 31 30 2E 36 0D 0A 32 30 31 35 20 31 32 20
|
150
|
+
|
151
|
+
20 37 20 31 39 20 20 35 39 20 20 38 2E 30 0D 0A
|
152
|
+
|
153
|
+
32 30 31 36 20 20 31 20 20 34 20 20 31 20 20 39
|
154
|
+
|
155
|
+
20 31 31 20 37 2E 35 0D 0A 32 30 31 36 20 20 31
|
156
|
+
|
157
|
+
20 20 20 34 20 31 39 20 31 36 20 20 38 20 20 39
|
158
|
+
|
159
|
+
2E 30 0D 0A 32 30 31 36 20 20 31 20 20 34 20 31
|
160
|
+
|
161
|
+
39 20 20 32 20 34 34 20 20 37 2E 35 0D 0A 32 30
|
162
|
+
|
163
|
+
31 36 20 20 31 20 20 34 20 32 32 20 33 31 20 35
|
164
|
+
|
165
|
+
32 20 39 2E 33 0D 0A 32 30 31 36 20 20 31 20 20
|
166
|
+
|
167
|
+
34 20 32 33 20 33 33 20 35 30 20 31 30 2E 32 0D
|
168
|
+
|
169
|
+
0A 32 30 31 36 20 20 31 20 33 31 20 32 31 20 20
|
170
|
+
|
171
|
+
39 20 35 33 20 20 37 2E 30 0D 0A 32 30 31 36 20
|
172
|
+
|
173
|
+
20 32 20 20 32 20 20 38 20 34 39 20 20 39 20 20
|
174
|
+
|
175
|
+
39 2E 34 0D 0A 32 30 31 36 20 20 32 20 32 37 20
|
176
|
+
|
177
|
+
20 31 32 20 34 35 20 32 36 20 31 33 2E 30 0D 0A
|
178
|
+
|
179
|
+
32 30 31 36 20 20 32 20 32 38 20 20 32 20 32 37
|
180
|
+
|
181
|
+
20 32 30 20 20 37 2E 34 0D 0A 32 30 31 36 20 20
|
182
|
+
|
183
|
+
35 20 20 32 20 31 36 20 34 33 20 33 32 20 20 36
|
184
|
+
|
185
|
+
2E 33 0D 0A 32 30 31 37 20 20 39 20 31 34 20 31
|
186
|
+
|
187
|
+
30 20 32 31 20 31 38 20 20 36 2E 30 0D 0A
|
148
188
|
|
149
189
|
実行結果は
|
150
190
|
|
5
手直し
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,14 +10,6 @@
|
|
10
10
|
|
11
11
|
```c言語
|
12
12
|
|
13
|
-
/* 構造体の配列を宣言して、ファイルからデータを読み込むサンプルプログラムです。
|
14
|
-
|
15
|
-
今回は入力までです。配列を関数へ渡して利用する例は次回に。
|
16
|
-
|
17
|
-
【 構造体配列にファイルからデータ入力するプログラム例 】
|
18
|
-
|
19
|
-
*/
|
20
|
-
|
21
13
|
#include <stdio.h>
|
22
14
|
|
23
15
|
#include <stdlib.h>
|
@@ -150,7 +142,7 @@
|
|
150
142
|
|
151
143
|
2017 9 14 10 21 18 6.0
|
152
144
|
|
153
|
-
これをバイナリデータに変換したつもりの"LACKNUM.DAT"を使って
|
145
|
+
これをバイナリデータに変換したつもりの"LACKNUM.DAT"を使って実行しています。
|
154
146
|
|
155
147
|
|
156
148
|
|
4
手直し
test
CHANGED
File without changes
|
test
CHANGED
@@ -220,7 +220,29 @@
|
|
220
220
|
|
221
221
|
C:\MinGW\users\chap09\kadai>
|
222
222
|
|
223
|
-
|
223
|
+
別のプログラムの実行結果
|
224
|
+
|
225
|
+
C:\MinGW\users\chap09\kadai>gcc -I. -o kadai9-1-2 kadai9-1-2.c pdcurses.a
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
C:\MinGW\users\chap09\kadai>kadai9-1-2
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
過去の履歴
|
234
|
+
|
235
|
+
--------------------------
|
236
|
+
|
237
|
+
221656476年 824195122月 540161312日 941629494時 824195360分 925970482秒
|
238
|
+
|
239
|
+
得点(所要時間)は0.0秒
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
221590947年 540094753月 958411040日 874524960時 909193266分 168636462秒
|
244
|
+
|
245
|
+
得点(所要時間)は0.0秒
|
224
246
|
|
225
247
|
|
226
248
|
|
3
無駄なコードの削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -88,16 +88,6 @@
|
|
88
88
|
|
89
89
|
fclose( fp );
|
90
90
|
|
91
|
-
|
92
|
-
|
93
|
-
// 本来なら、入力したデータを関数などに渡して使います
|
94
|
-
|
95
|
-
// たとえば一番背の高い人を調べる関数
|
96
|
-
|
97
|
-
// int whoIsTallest( shape_t stars[], int num ) など
|
98
|
-
|
99
|
-
// 関数との受け渡しは次回のTipsで ...
|
100
|
-
|
101
91
|
|
102
92
|
|
103
93
|
if( (fp=fopen( "LACKNUM.DAT", "r")) == NULL ) {
|
@@ -108,28 +98,6 @@
|
|
108
98
|
|
109
99
|
}
|
110
100
|
|
111
|
-
|
112
|
-
|
113
|
-
/* for(i = 0; i < MAXCNT; i++ ){
|
114
|
-
|
115
|
-
if((fread(&stars, sizeof(stars), 1, fp)!=0)){
|
116
|
-
|
117
|
-
printf("%d年 %d月 %d日 %d時 %d分 %d秒\n",
|
118
|
-
|
119
|
-
stars->tm_year + 1900, stars->tm_mon + 1,
|
120
|
-
|
121
|
-
stars->tm_mday, stars->tm_hour, stars->tm_min, stars->tm_sec);
|
122
|
-
|
123
|
-
fread(&score, sizeof(double), 1, fp);
|
124
|
-
|
125
|
-
printf("得点(所要時間)は%.1f秒\n\n", score);
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
*/
|
132
|
-
|
133
101
|
printf("count%d\n",count);
|
134
102
|
|
135
103
|
for(i = 0; i < count; i++ ){
|
@@ -138,15 +106,9 @@
|
|
138
106
|
|
139
107
|
, stars[i].tm_year,stars[i].tm_mon,stars[i].tm_mday,
|
140
108
|
|
141
|
-
stars[i].tm_hour,stars[i].tm_min,stars[i].tm_sec,stars[i].score);
|
109
|
+
stars[i].tm_hour,stars[i].tm_min,stars[i].tm_sec,stars[i].score);
|
142
|
-
|
143
|
-
|
144
|
-
|
110
|
+
|
145
|
-
}
|
111
|
+
}
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
112
|
|
151
113
|
fclose(fp);
|
152
114
|
|
2
説明の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -162,17 +162,33 @@
|
|
162
162
|
|
163
163
|
データファイル starsdata.txt を下記とすると
|
164
164
|
|
165
|
-
|
165
|
+
2017 9 16 8 52 18 10.6
|
166
|
+
|
166
|
-
|
167
|
+
2015 12 7 19 9 59 8.0
|
168
|
+
|
169
|
+
2016 1 4 1 9 11 7.5
|
170
|
+
|
171
|
+
2016 1 4 19 16 8 9.0
|
172
|
+
|
173
|
+
2016 1 4 19 2 44 7.5
|
174
|
+
|
175
|
+
2016 1 4 22 31 52 9.3
|
176
|
+
|
177
|
+
2016 1 4 23 33 50 10.2
|
178
|
+
|
179
|
+
2016 1 31 21 9 53 7.0
|
180
|
+
|
181
|
+
2016 2 2 8 49 9.4
|
182
|
+
|
167
|
-
|
183
|
+
2016 2 27 12 4E 13.0
|
168
|
-
|
169
|
-
|
184
|
+
|
170
|
-
|
171
|
-
|
185
|
+
2016 2 28 2 27 20 7.4
|
172
|
-
|
186
|
+
|
173
|
-
|
187
|
+
2016 5 2 16 43 6.3
|
174
|
-
|
188
|
+
|
175
|
-
|
189
|
+
2017 9 14 10 21 18 6.0
|
190
|
+
|
191
|
+
これをバイナリデータに変換したつもりの"LACKNUM.DAT"を使ってじっこうしています。
|
176
192
|
|
177
193
|
|
178
194
|
|
1
説明文の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,26 @@
|
|
10
10
|
|
11
11
|
```c言語
|
12
12
|
|
13
|
+
/* 構造体の配列を宣言して、ファイルからデータを読み込むサンプルプログラムです。
|
14
|
+
|
15
|
+
今回は入力までです。配列を関数へ渡して利用する例は次回に。
|
16
|
+
|
17
|
+
【 構造体配列にファイルからデータ入力するプログラム例 】
|
18
|
+
|
19
|
+
*/
|
20
|
+
|
21
|
+
#include <stdio.h>
|
22
|
+
|
23
|
+
#include <stdlib.h>
|
24
|
+
|
25
|
+
#include <time.h>
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
#define MAXCNT 100
|
30
|
+
|
31
|
+
|
32
|
+
|
13
33
|
typedef struct { // 構造体の宣言
|
14
34
|
|
15
35
|
int tm_year;
|
@@ -32,6 +52,196 @@
|
|
32
52
|
|
33
53
|
|
34
54
|
|
55
|
+
int main()
|
56
|
+
|
57
|
+
{
|
58
|
+
|
59
|
+
int i, count=0;
|
60
|
+
|
61
|
+
FILE *fp;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
score_data stars[MAXCNT]; // 構造体配列の宣言
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
if( (fp=fopen( "LACKNUM.DAT", "r" )) == NULL ) {
|
70
|
+
|
71
|
+
printf( "ファイルがオープンできません\n" );
|
72
|
+
|
73
|
+
exit( 1 );
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
for( i = 0; i < MAXCNT; i++ ) {
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
if( fscanf( fp, "%d %d %d %d %d %d %lf\n" // 構造体配列への読み込み
|
82
|
+
|
83
|
+
, &stars[i].tm_year,& stars[i].tm_mon, &stars[i].tm_mday, &stars[i].tm_hour, &stars[i].tm_min,&stars[i].tm_sec,&stars[i].score ) != 7 ) break;
|
84
|
+
|
85
|
+
count++;
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
fclose( fp );
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
// 本来なら、入力したデータを関数などに渡して使います
|
94
|
+
|
95
|
+
// たとえば一番背の高い人を調べる関数
|
96
|
+
|
97
|
+
// int whoIsTallest( shape_t stars[], int num ) など
|
98
|
+
|
99
|
+
// 関数との受け渡しは次回のTipsで ...
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
if( (fp=fopen( "LACKNUM.DAT", "r")) == NULL ) {
|
104
|
+
|
105
|
+
printf( "ファイルがオープンできません\n" );
|
106
|
+
|
107
|
+
exit( 1 );
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
/* for(i = 0; i < MAXCNT; i++ ){
|
114
|
+
|
115
|
+
if((fread(&stars, sizeof(stars), 1, fp)!=0)){
|
116
|
+
|
117
|
+
printf("%d年 %d月 %d日 %d時 %d分 %d秒\n",
|
118
|
+
|
119
|
+
stars->tm_year + 1900, stars->tm_mon + 1,
|
120
|
+
|
121
|
+
stars->tm_mday, stars->tm_hour, stars->tm_min, stars->tm_sec);
|
122
|
+
|
123
|
+
fread(&score, sizeof(double), 1, fp);
|
124
|
+
|
125
|
+
printf("得点(所要時間)は%.1f秒\n\n", score);
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
*/
|
132
|
+
|
133
|
+
printf("count%d\n",count);
|
134
|
+
|
135
|
+
for(i = 0; i < count; i++ ){
|
136
|
+
|
137
|
+
printf( "%d年 %d月 %d日 %d時 %d分 %d秒 \n所要時間(score)は%.1f\n" // 構造体配列への読み込み
|
138
|
+
|
139
|
+
, stars[i].tm_year,stars[i].tm_mon,stars[i].tm_mday,
|
140
|
+
|
141
|
+
stars[i].tm_hour,stars[i].tm_min,stars[i].tm_sec,stars[i].score);
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
fclose(fp);
|
152
|
+
|
153
|
+
return 0;
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
/*
|
162
|
+
|
163
|
+
データファイル starsdata.txt を下記とすると
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
Brad_Pitt 183.2 73.4
|
168
|
+
|
169
|
+
Tom_Cruise 170.1 67.2
|
170
|
+
|
171
|
+
Johnny_Depp 178.2 70.5
|
172
|
+
|
173
|
+
Will_Smith 188.0 78.3
|
174
|
+
|
175
|
+
Bruce_Willis 184.0 78.3
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
実行結果は
|
180
|
+
|
181
|
+
C:\MinGW\users\chap09\kadai>gcc -I. -o test4 test4.c -Wall
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
C:\MinGW\users\chap09\kadai>test4
|
186
|
+
|
187
|
+
count13
|
188
|
+
|
189
|
+
2017年 9月 16日 8時 52分 18秒
|
190
|
+
|
191
|
+
所要時間(score)は10.6
|
192
|
+
|
193
|
+
2015年 12月 7日 19時 9分 59秒
|
194
|
+
|
195
|
+
所要時間(score)は8.0
|
196
|
+
|
197
|
+
2016年 1月 4日 1時 9分 11秒
|
198
|
+
|
199
|
+
所要時間(score)は7.5
|
200
|
+
|
201
|
+
2016年 1月 4日 19時 16分 8秒
|
202
|
+
|
203
|
+
所要時間(score)は9.0
|
204
|
+
|
205
|
+
2016年 1月 4日 19時 2分 44秒
|
206
|
+
|
207
|
+
所要時間(score)は7.5
|
208
|
+
|
209
|
+
2016年 1月 4日 22時 31分 52秒
|
210
|
+
|
211
|
+
所要時間(score)は9.3
|
212
|
+
|
213
|
+
2016年 1月 4日 23時 33分 50秒
|
214
|
+
|
215
|
+
所要時間(score)は10.2
|
216
|
+
|
217
|
+
2016年 1月 31日 21時 9分 53秒
|
218
|
+
|
219
|
+
所要時間(score)は7.0
|
220
|
+
|
221
|
+
2016年 2月 2日 8時 49分 9秒
|
222
|
+
|
223
|
+
所要時間(score)は9.4
|
224
|
+
|
225
|
+
2016年 2月 27日 12時 45分 26秒
|
226
|
+
|
227
|
+
所要時間(score)は13.0
|
228
|
+
|
229
|
+
2016年 2月 28日 2時 27分 20秒
|
230
|
+
|
231
|
+
所要時間(score)は7.4
|
232
|
+
|
233
|
+
2016年 5月 2日 16時 43分 32秒
|
234
|
+
|
235
|
+
所要時間(score)は6.3
|
236
|
+
|
237
|
+
2017年 9月 14日 10時 21分 18秒
|
238
|
+
|
239
|
+
所要時間(score)は6.0
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
C:\MinGW\users\chap09\kadai>
|
244
|
+
|
35
245
|
|
36
246
|
|
37
247
|
|