質問編集履歴

1

読み込みたいファイル、mainを追加しました。

2017/09/19 09:33

投稿

ssssskkkkk
ssssskkkkk

スコア20

test CHANGED
File without changes
test CHANGED
@@ -1,521 +1,601 @@
1
+ ###ファイルの分割ロードについて
2
+
3
+ c++でファイルの分割ロードを実現したいと考えています。
4
+
5
+ 具体的には、毎フレーム128キロバイトずつ、もしくはまだ読み込めていないサイズの合計が128キロバイト未満であれば、残りサイズを読み込みます。
6
+
7
+
8
+
9
+ とある本に書いてあったコードを参考に、以下のようなコードを書いてみました。
10
+
11
+
12
+
13
+ ###Fileクラス
14
+
15
+
16
+
17
+ 実際に読み込むファイルのデータを保持しています。
18
+
19
+
20
+
21
+ ```c++
22
+
23
+ #ifndef _FILE_H_
24
+
25
+ #define _FILE_H_
26
+
27
+
28
+
29
+ /*
30
+
31
+
32
+
33
+ File.h
34
+
35
+
36
+
37
+ */
38
+
39
+
40
+
41
+ #include "Singleton.h"
42
+
43
+ #include <string>
44
+
45
+
46
+
47
+ using namespace std;
48
+
49
+
50
+
51
+ class File {
52
+
53
+
54
+
55
+ public:
56
+
57
+
58
+
59
+ bool isReady() const;
60
+
61
+ int size() const;
62
+
63
+ const char* data() const;
64
+
65
+ string fileName() const;
66
+
67
+
68
+
69
+ File(const char* fileName);
70
+
71
+ ~File() {};
72
+
73
+
74
+
75
+ private:
76
+
77
+
78
+
79
+ friend class Loader;
80
+
81
+
82
+
83
+ string mFileName;
84
+
85
+ char* mData;
86
+
87
+ int mSize;
88
+
89
+
90
+
91
+ };
92
+
93
+
94
+
95
+ #endif
96
+
97
+
98
+
99
+
100
+
101
+ ```
102
+
103
+
104
+
105
+ ```c++
106
+
107
+ #include "File.h"
108
+
109
+
110
+
111
+ /*
112
+
113
+
114
+
115
+ File.cpp
116
+
117
+
118
+
119
+ */
120
+
121
+
122
+
123
+ bool File::isReady() const {
124
+
125
+
126
+
127
+ return mData != 0;
128
+
129
+
130
+
131
+ }
132
+
133
+
134
+
135
+ int File::size() const {
136
+
137
+
138
+
139
+ return mSize;
140
+
141
+
142
+
143
+ }
144
+
145
+
146
+
147
+ const char* File::data() const {
148
+
149
+
150
+
151
+ return mData;
152
+
153
+
154
+
155
+ }
156
+
157
+
158
+
159
+ string File::fileName() const {
160
+
161
+
162
+
163
+ return mFileName;
164
+
165
+
166
+
167
+ }
168
+
169
+
170
+
171
+ File::File(const char* fileName) {
172
+
173
+
174
+
175
+ mFileName = fileName;
176
+
177
+
178
+
179
+ mData = 0;
180
+
181
+
182
+
183
+ }
184
+
185
+ ```
186
+
187
+
188
+
189
+
190
+
191
+ ###Loaderクラス
192
+
193
+
194
+
195
+ ファイルのロードを行うクラスです。
196
+
197
+ createFileで読み込みたいファイルのListであるmFilesにファイルの登録を行います。
198
+
199
+ updateを毎フレーム呼び出し、少しずつファイルを読み込みます。
200
+
201
+
202
+
203
+
204
+
205
+ ```c++
206
+
207
+
208
+
209
+ #ifndef _LOADER_H_
210
+
211
+ #define _LOADER_H_
212
+
213
+
214
+
215
+ /*
216
+
217
+
218
+
219
+ Loader.h
220
+
221
+
222
+
223
+ */
224
+
225
+
226
+
227
+ #include "Singleton.h"
228
+
229
+ #include "File.h"
230
+
231
+ #include <list>
232
+
233
+ #include <fstream>
234
+
235
+
236
+
237
+ class Loader : public Singleton<Loader> {
238
+
239
+
240
+
241
+ public:
242
+
243
+
244
+
245
+ void createFile(File** file, const char* fileName);
246
+
247
+ void destroyFile(const char* fileName);
248
+
249
+ void update();
250
+
251
+
252
+
253
+ private:
254
+
255
+
256
+
257
+ friend class Singleton<Loader>;
258
+
259
+
260
+
261
+ Loader() {}
262
+
263
+ Loader(const Loader& obj) {}
264
+
265
+ void operator=(const Loader& obj) {}
266
+
267
+ ~Loader() {}
268
+
269
+
270
+
271
+ list<File*> mFiles;
272
+
273
+
274
+
275
+ size_t mFileSize;
276
+
277
+ size_t mSeekPosition;
278
+
279
+ ifstream* mStream;
280
+
281
+ File* mCurrentFile;
282
+
283
+ char* mData;
284
+
285
+
286
+
287
+
288
+
289
+ };
290
+
291
+
292
+
293
+ #endif
294
+
295
+
296
+
297
+ ```
298
+
299
+
300
+
301
+ ```c++
302
+
303
+
304
+
305
+ #include "Loader.h"
306
+
307
+ #include "DEBUG.h"
308
+
309
+
310
+
311
+ /*
312
+
313
+
314
+
315
+ Loader.cpp
316
+
317
+
318
+
319
+ */
320
+
321
+
322
+
323
+ void Loader::createFile(File** file, const char* fileName) {
324
+
325
+
326
+
327
+ ASSERT((!(*file) && "ポインタはNULLである必要があります"));
328
+
329
+
330
+
331
+ *file = new File(fileName);
332
+
333
+
334
+
335
+ mFiles.push_back(*file);
336
+
337
+
338
+
339
+ }
340
+
341
+
342
+
343
+ void Loader::destroyFile(const char* fileName) {
344
+
345
+
346
+
347
+ bool isFound = false;
348
+
349
+
350
+
351
+ for (auto itr = mFiles.begin(); itr != mFiles.end(); ++itr) {
352
+
353
+
354
+
355
+ File* file = *itr;
356
+
357
+
358
+
359
+ if (file->mFileName == fileName) {
360
+
361
+
362
+
363
+ delete file;
364
+
365
+
366
+
367
+ file = nullptr;
368
+
369
+
370
+
371
+ mFiles.erase(itr);
372
+
373
+
374
+
375
+ isFound = true;
376
+
377
+
378
+
379
+ break;
380
+
381
+
382
+
383
+ }
384
+
385
+
386
+
387
+ }
388
+
389
+
390
+
391
+ ASSERT((isFound && "見つかりませんでした"));
392
+
393
+
394
+
395
+ }
396
+
397
+
398
+
399
+ void Loader::update() {
400
+
401
+
402
+
403
+ if (mCurrentFile == nullptr && !mFiles.empty()) {
404
+
405
+
406
+
407
+ auto itr = mFiles.begin();
408
+
409
+
410
+
411
+ mCurrentFile = *itr;
412
+
413
+
414
+
415
+ mStream = new ifstream(mCurrentFile->mFileName);
416
+
417
+
418
+
419
+ mFileSize = static_cast<size_t>(mStream->seekg(0, fstream::end).tellg());
420
+
421
+
422
+
423
+ mData = new char[mFileSize];
424
+
425
+
426
+
427
+ }
428
+
429
+
430
+
431
+ if (mCurrentFile != nullptr) {
432
+
433
+
434
+
435
+ int rest = mFileSize - mSeekPosition; // ファイルサイズ - 現在の読み込み位置で、残りのファイルサイズを取得しています。
436
+
437
+
438
+
439
+ int maxKB = 128 * 1024;
440
+
441
+
442
+
443
+ int size = (rest > maxKB) ? maxKB : rest;
444
+
445
+
446
+
447
+ mStream->seekg(mSeekPosition, fstream::beg);
448
+
449
+
450
+
451
+ mStream->read(mData + mSeekPosition, size);
452
+
453
+
454
+
455
+ mSeekPosition += size;
456
+
457
+
458
+
459
+ if (size == rest) {
460
+
461
+
462
+
463
+ mCurrentFile->mData = mData;
464
+
465
+ mCurrentFile->mSize = mFileSize;
466
+
467
+
468
+
469
+ delete mData;
470
+
471
+ mData = 0;
472
+
473
+
474
+
475
+ mFileSize = mSeekPosition = 0;
476
+
477
+
478
+
479
+ delete mStream;
480
+
481
+
482
+
483
+ mFiles.erase(mFiles.begin());
484
+
485
+
486
+
487
+ mCurrentFile = nullptr;
488
+
489
+
490
+
491
+ }
492
+
493
+
494
+
495
+ }
496
+
497
+
498
+
499
+ }
500
+
501
+
502
+
503
+ ```
504
+
505
+
506
+
507
+ ###読み込むファイル
508
+
509
+
510
+
511
+ Test.txtというファイルを読み込みます。
512
+
513
+ 今回は、試しにファイルサイズが小さいものを読み込んでいるので、あまり分割する意味はありませんが、実際にはもっと大きいファイルを読み込みたいと考えています。
514
+
515
+ ```Test.txt
516
+
517
+ TEST
518
+
519
+ GAMEPROGRAMING
520
+
521
+ ```
522
+
523
+
524
+
1
525
  ###実現したいこと
2
526
 
3
- c++でファイルの分割ロードを実現したいと考えています。
527
+
4
-
5
- 具体的には、毎フレーム128キロバイトずつ、もしくはまだ読み込めていないサイズの合計が128キロバイト未満であれば、残りサイズを読み込みます。
528
+
6
-
7
-
8
-
9
- とある本てあったコード参考に、以下のようなコードを書いてみました
529
+ 上記のTest.txtを読み込み、標準出力表示したと思い、mainを以下のように実装したのですが、
530
+
531
+ 上手くいきませんでした。
10
532
 
11
533
 
12
534
 
13
535
  ```c++
14
536
 
15
-
16
-
17
- #ifndef _LOADER_H_
18
-
19
- #define _LOADER_H_
20
-
21
-
22
-
23
- /*
24
-
25
-
26
-
27
- Loader.h
28
-
29
-
30
-
31
- ファイルのロードを行うクラスです。
32
-
33
-
34
-
35
- createFileで読み込みたいファイルのListであるmFilesにファイルの登録を行います。
36
-
37
-
38
-
39
- updateを毎フレーム呼び出し、少しずつファイルを読み込みます。
40
-
41
-
42
-
43
- */
44
-
45
-
46
-
47
- #include "Singleton.h"
537
+ #include "Loader.h"
48
538
 
49
539
  #include "File.h"
50
540
 
51
- #include <list>
52
-
53
- #include <fstream>
54
-
55
-
56
-
57
- class Loader : public Singleton<Loader> {
58
-
59
-
60
-
61
- public:
62
-
63
-
64
-
65
- void createFile(File** file, const char* fileName);
66
-
67
- void destroyFile(const char* fileName);
68
-
69
- void update();
70
-
71
-
72
-
73
- private:
74
-
75
-
76
-
77
- friend class Singleton<Loader>;
78
-
79
-
80
-
81
- Loader() {}
82
-
83
- Loader(const Loader& obj) {}
84
-
85
- void operator=(const Loader& obj) {}
86
-
87
- ~Loader() {}
88
-
89
-
90
-
91
- list<File*> mFiles;
92
-
93
-
94
-
95
- size_t mFileSize;
96
-
97
- size_t mSeekPosition;
98
-
99
- ifstream* mStream;
100
-
101
- File* mCurrentFile;
102
-
103
- char* mData;
104
-
105
-
106
-
107
-
108
-
109
- };
110
-
111
-
112
-
113
- #endif
114
-
115
-
541
+ #include <iostream>
542
+
543
+ #include <string>
544
+
545
+
546
+
547
+ using namespace std;
548
+
549
+
550
+
551
+ int main(void) {
552
+
553
+
554
+
555
+ File* file = nullptr;
556
+
557
+
558
+
559
+ if (file == nullptr) {
560
+
561
+
562
+
563
+ Loader::shared().createFile(&file, "Test.txt"); // shared()はシングルトンクラスによって提供されている、唯一のインスタスを返すメソッドです。
564
+
565
+
566
+
567
+ }
568
+
569
+
570
+
571
+ while (!file->isReady()) { //isReady()は読み込みが終わっているならばtrueを返すので、読み込みが終わっていない間、回ります。
572
+
573
+
574
+
575
+ Loader::shared().update();
576
+
577
+
578
+
579
+ }
580
+
581
+
582
+
583
+ string str(file->data());
584
+
585
+
586
+
587
+ cout << str << endl;
588
+
589
+
590
+
591
+ return 0;
592
+
593
+
594
+
595
+ }
116
596
 
117
597
  ```
118
598
 
119
599
 
120
600
 
121
- ```c++
122
-
123
-
124
-
125
- #include "Loader.h"
126
-
127
- #include "DEBUG.h"
128
-
129
-
130
-
131
- /*
132
-
133
-
134
-
135
- Loader.cpp
136
-
137
-
138
-
139
- */
140
-
141
-
142
-
143
- void Loader::createFile(File** file, const char* fileName) {
144
-
145
-
146
-
147
- ASSERT((!(*file) && "ポインタはNULLである必要があります"));
148
-
149
-
150
-
151
- *file = new File(fileName);
152
-
153
-
154
-
155
- mFiles.push_back(*file);
156
-
157
-
158
-
159
- }
160
-
161
-
162
-
163
- void Loader::destroyFile(const char* fileName) {
164
-
165
-
166
-
167
- bool isFound = false;
168
-
169
-
170
-
171
- for (auto itr = mFiles.begin(); itr != mFiles.end(); ++itr) {
172
-
173
-
174
-
175
- File* file = *itr;
176
-
177
-
178
-
179
- if (file->mFileName == fileName) {
180
-
181
-
182
-
183
- delete file;
184
-
185
-
186
-
187
- file = nullptr;
188
-
189
-
190
-
191
- mFiles.erase(itr);
192
-
193
-
194
-
195
- isFound = true;
196
-
197
-
198
-
199
- break;
200
-
201
-
202
-
203
- }
204
-
205
-
206
-
207
- }
208
-
209
-
210
-
211
- ASSERT((isFound && "見つかりませんでした"));
212
-
213
-
214
-
215
- }
216
-
217
-
218
-
219
- void Loader::update() {
220
-
221
-
222
-
223
- if (mCurrentFile == nullptr && !mFiles.empty()) {
224
-
225
-
226
-
227
- auto itr = mFiles.begin();
228
-
229
-
230
-
231
- mCurrentFile = *itr;
232
-
233
-
234
-
235
- mStream = new ifstream(mCurrentFile->mFileName);
236
-
237
-
238
-
239
- mFileSize = static_cast<size_t>(mStream->seekg(0, fstream::end).tellg());
240
-
241
-
242
-
243
- mData = new char[mFileSize];
244
-
245
-
246
-
247
- }
248
-
249
-
250
-
251
- if (mCurrentFile != nullptr) {
252
-
253
-
254
-
255
- int rest = mFileSize - mSeekPosition;
256
-
257
-
258
-
259
- int maxKB = 128 * 1024;
260
-
261
-
262
-
263
- int size = (rest > maxKB) ? maxKB : rest;
264
-
265
-
266
-
267
- mStream->seekg(mSeekPosition, fstream::beg);
268
-
269
-
270
-
271
- mStream->read(mData + mSeekPosition, size);
272
-
273
-
274
-
275
- mSeekPosition += size;
276
-
277
-
278
-
279
- if (size == rest) {
280
-
281
-
282
-
283
- mCurrentFile->mData = mData;
284
-
285
- mCurrentFile->mSize = mFileSize;
286
-
287
-
288
-
289
- delete mData;
290
-
291
- mData = 0;
292
-
293
-
294
-
295
- mFileSize = mSeekPosition = 0;
296
-
297
-
298
-
299
- delete mStream;
300
-
301
-
302
-
303
- mFiles.erase(mFiles.begin());
304
-
305
-
306
-
307
- mCurrentFile = nullptr;
308
-
309
-
310
-
311
- }
312
-
313
-
314
-
315
- }
316
-
317
-
318
-
319
- }
320
-
321
-
322
-
323
- ```
324
-
325
- ```c++
326
-
327
- #ifndef _FILE_H_
328
-
329
- #define _FILE_H_
330
-
331
-
332
-
333
- /*
334
-
335
-
336
-
337
- File.h
338
-
339
-
340
-
341
- */
342
-
343
-
344
-
345
- #include "Singleton.h"
346
-
347
- #include <string>
348
-
349
-
350
-
351
- using namespace std;
352
-
353
-
354
-
355
- class File {
356
-
357
-
358
-
359
- public:
360
-
361
-
362
-
363
- bool isReady() const;
364
-
365
- int size() const;
366
-
367
- const char* data() const;
368
-
369
- string fileName() const;
370
-
371
-
372
-
373
- File(const char* fileName);
374
-
375
- ~File() {};
376
-
377
-
378
-
379
- private:
380
-
381
-
382
-
383
- friend class Loader;
384
-
385
-
386
-
387
- string mFileName;
388
-
389
- char* mData;
390
-
391
- int mSize;
392
-
393
-
394
-
395
- };
396
-
397
-
398
-
399
- #endif
400
-
401
-
402
-
403
-
404
-
405
- ```
406
-
407
-
408
-
409
- ```c++
410
-
411
- #include "File.h"
412
-
413
-
414
-
415
- /*
416
-
417
-
418
-
419
- File.cpp
420
-
421
-
422
-
423
- */
424
-
425
-
426
-
427
- bool File::isReady() const {
428
-
429
-
430
-
431
- return mData != 0;
432
-
433
-
434
-
435
- }
436
-
437
-
438
-
439
- int File::size() const {
440
-
441
-
442
-
443
- return mSize;
444
-
445
-
446
-
447
- }
448
-
449
-
450
-
451
- const char* File::data() const {
452
-
453
-
454
-
455
- return mData;
456
-
457
-
458
-
459
- }
460
-
461
-
462
-
463
- string File::fileName() const {
464
-
465
-
466
-
467
- return mFileName;
468
-
469
-
470
-
471
- }
472
-
473
-
474
-
475
- File::File(const char* fileName) {
476
-
477
-
478
-
479
- mFileName = fileName;
480
-
481
-
482
-
483
- mData = 0;
484
-
485
-
486
-
487
- }
488
-
489
- ```
490
-
491
-
492
-
493
-
494
-
495
- SingletonやDEBUGは割愛します。
496
-
497
-
498
-
499
-
500
-
501
- ###発生している問題
502
-
503
-
504
-
505
- 特にエラーメッセージが出ているわけではないのですが、このコードで最終的にFileクラスのmDataに読み込んだデータが格納されると踏んでいるのですが、そのような理解で合っているのでしょうか。
506
-
507
-
508
-
509
- また、上記のような理解であっている場合、FileクラスのmDataにはどのような使い道があるのでしょうか。
510
-
511
-
512
-
513
- 例えば、txt形式のファイルを読み込んだ場合、どのようにして、mDataからtxt形式のファイルの内容を取り出すことができるのしょうか。
514
-
515
-
516
-
517
- そもそも、上記のコードでは、読み込みが実現できていない場合は、そちらもご指摘いただけると助かります。
518
-
519
-
520
-
521
- いくつも質問てしまってお手数をおけしますが、よろしくお願いします。
601
+ どのようにすれば実現できるのでょうよろしくお願いいたします。