OSDN Git Service

ff1a447c006e0ade22293dfcd6c7df7aaec82f9d
[ring-lang-081/ring.git] / docs / build / html / _sources / files.txt
1 .. index:: 
2         single: ファイル; はじめに
3
4 ========
5 ファイル
6 ========
7
8 ファイル関数の用法を学びます。
9
10 * Read()
11 * Write()
12 * Dir()
13 * Rename()
14 * Remove()
15 * fopen()
16 * fclose()
17 * fflush()
18 * freopen()
19 * tempfile()
20 * tempname()
21 * fseek()
22 * ftell()
23 * rewind()
24 * fgetpos()
25 * fsetpos()
26 * clearerr()
27 * feof()
28 * ferror()
29 * perror()
30 * fgetc()
31 * fgets()
32 * fputc()
33 * fputs()
34 * ungetc()
35 * fread()
36 * fwrite()
37 * fexists()
38 * 数値とバイト
39
40 .. index:: 
41         pair: ファイル; Read() 関数によるファイル内容の読み取り
42
43 Read() 関数
44 ===========
45
46 Read() 関数はファイルの内容を読み取ります。
47
48 文法:
49
50 .. code-block:: ring
51
52         Read(cFileName) ---> 文字列はファイルの内容を有しています。
53
54 用例:
55
56 .. code-block:: ring
57
58         see read("myfile.txt")
59
60 Read() 関数はバイナリファイルの読み取りもできます。
61
62 用例:
63
64 .. code-block:: ring
65
66         see read("myapp.exe")
67
68
69 .. index:: 
70         pair: ファイル; Write() 関数によるファイルへの書き込み
71
72 Write() 関数
73 ============
74
75 Write() 関数は文字列をファイルへ書き込みます。
76
77 文法:
78
79 .. code-block:: ring
80
81         Write(cFileName,cString)        # 文字列 cString をファイル cFileName へ書き込みます。
82
83 Write() 関数はバイナリファイルの書き込みもできます。
84
85 用例:
86
87 .. code-block:: ring
88
89         # ファイルのコピー
90         cFile = read("ring.exe")
91         write("ring2.exe",cFile)
92
93 .. index:: 
94         pair: ファイル; Dir()
95
96 Dir() 関数
97 ==========
98
99 Dir() 関数はフォルダの内容 (ファイルとサブフォルダ) を取得します。
100
101 文法:
102
103 .. code-block:: ring
104
105         Dir(cFolderPath) ---> リストはファイルとサブフォルダを有しています。
106
107 この関数はリストを返します。リストごとの項目は二つの項目を有するリストになります。
108
109 * ファイル / サブフォルダの名前
110
111 * 種類 (0 = ファイル , 1 = フォルダ、ディレクトリ)
112
113 用例:
114
115 .. code-block:: ring
116
117         see "Testing DIR() " + nl
118         mylist = dir("C:\myfolder")
119         for x in mylist
120                 if x[2] 
121                         see "Directory : " + x[1] + nl
122                 else
123                         see "File : " + x[1] + nl
124                 ok
125         next
126         see "Files count : " + len(mylist)
127
128 .. index:: 
129         pair: ファイル; Rename()
130
131 Rename() 関数
132 =============
133
134 Rename() 関数はファイルを名称変更します。
135
136 文法:
137
138 .. code-block:: ring
139
140         Rename(cOldFileName,cNewFileName) ---> 数値 ( 状態: 成功 (0) , エラー (-1) )
141
142 用例:
143
144 .. code-block:: ring
145
146         rename("file.txt","help.txt")
147
148 .. index:: 
149         pair: ファイル; Remove()
150
151 Remove() 関数
152 =============
153
154 Remove() 関数はファイルを削除します。
155
156
157 文法:
158
159 .. code-block:: ring
160
161         Remove(cFileName)
162
163 用例:
164
165 .. code-block:: ring
166
167         remove("test.txt")
168
169 .. index:: 
170         pair: ファイル; Fopen()
171
172 Fopen() 関数
173 ============
174
175 Fopen() 関数はファイルを開きます。
176
177 文法:
178
179 .. code-block:: ring
180
181         Fopen(cFileName,cMode) ---> ファイルハンドル
182
183 .. csv-table::
184         :header: "モード", "説明"
185         :widths:       20,     80
186
187         "“r”",      "読み取り (ファイルが存在している必要があります)"
188         "“w”",      "書き込み (空のファイルを作成、上書き)"
189         "“a”",      "追記 (ファイルが存在しない場合は作成)"
190         "“r+”",     "更新 (読み取り、書き込み)"
191         "“w+”",     "空のファイルを作成 (読み取り、書き込み)"
192         "“a+”",     "読み取りと追記"
193
194 .. index:: 
195         pair: ファイル; Fclose()
196
197 Fclose() 関数
198 =============
199
200 fopen() 関数でファイルを開いて処理を終えた後は、
201 Fclose() 関数でファイルを閉じます。
202
203 文法:
204
205 .. code-block:: ring
206
207         Fclose(ファイルハンドル)
208
209 .. index:: 
210         pair: ファイル; Fflush()
211
212 Fflush() 関数
213 =============
214
215 Fflush() 関数はストリームの出力バッファを追い出します (flush)。
216
217 文法:
218
219 .. code-block:: ring
220
221         Fflush(ファイルハンドル)
222
223 .. index:: 
224         pair: ファイル; Freopen()
225
226 Freopen() 関数
227 ==============
228
229 同じファイルハンドルで別のファイルを開きますが、
230 以前に開いていたファイルを併せて閉じます。
231
232 文法:
233
234 .. code-block:: ring
235
236         Freopen(cFileName,cMode,file handle) ---> ファイルハンドル
237
238 用例:
239
240 .. code-block:: ring
241
242         freopen("myprogoutput.txt","w+",stdout)
243         see "welcome" + nl
244         for x = 1 to 10
245                 see x + nl
246         next
247
248         ##
249         ## 必読 : https://ja.wikipedia.org/wiki/デバイスファイル#MS-DOS・Windows
250         ## このコードには移植性がありません。使用前に iswindows() で動作環境を
251         ## 判定してから各オペレーティングシステム専用のコードを書いてください。
252         ##
253
254         freopen("CON","w",stdout)   # Microsoft Windows では
255         see "Done" + nl             # もう一度、標準出力へ表示
256
257 実行結果:
258
259 .. code-block:: ring
260
261         # 標準出力へ出力
262         Done
263
264         # ファイルへの出力 : myprogoutput.txt
265         welcome
266         1
267         2
268         3
269         4
270         5
271         6
272         7
273         8
274         9
275         10
276
277 .. index:: 
278         pair: ファイル; Tempfile()
279
280 Tempfile() 関数
281 ===============
282
283 Tempfile() 関数は一時ファイルを作成します (バイナリ形式)。
284
285 ファイルはストリームが閉じられたときに自動で削除されます。
286
287 文法:
288
289 .. code-block:: ring
290
291         TempFile() ---> ファイルハンドル
292
293 .. index:: 
294         pair: ファイル; Tempname()
295
296 Tempname() 関数
297 ===============
298
299 Tempname() 関数は一時ファイル名を生成します。
300
301 生成された名前は唯一無二であり、既存ファイル名とは一切異なります。
302
303 文法:
304
305 .. code-block:: ring
306
307         Tempname() ---> ファイル名として生成された文字列
308
309 .. index:: 
310         pair: ファイル; Fseek()
311
312 Fseek() 関数
313 ============
314
315 Fseek() 関数はストリームにおけるファイル位置を設定します。
316
317 文法:
318
319 .. code-block:: ring
320
321         Fseek(ファイルハンドル, nOffset, nWhence) ---> 0 ならば成功です。
322
323 この表は nWhence の値を表したものです。
324
325 ==      ===============================
326 値     説明
327 ==      ===============================
328 0       ファイルの先頭
329 1       現在の位置
330 2       ファイルの末尾
331 ==      ===============================
332
333 .. index:: 
334         pair: ファイル; Ftell()
335
336 Ftell() 関数
337 ============
338
339 Ftell() 関数 はストリームにおける現在のファイル位置を検出します。 
340
341 文法:
342
343 .. code-block:: ring
344
345         Ftell(ファイルハンドル) ---> 数値によるファイルの位置です。
346
347 .. index:: 
348         pair: ファイル; Rewind()
349
350 Rewind() 関数
351 =============
352
353 Rewind() 関数はファイルの位置を先頭へ設定します。
354
355 文法:
356
357 .. code-block:: ring
358         
359         Rewind(ファイルハンドル)
360
361 .. index:: 
362         pair: ファイル; Fgetpos()
363
364 Fgetpos() 関数
365 ==============
366
367 Fgetpos() 関数はハンドルにおける現在のファイル位置を取得します。
368
369 文法:
370
371 .. code-block:: ring
372
373         Fgetpos(ファイルハンドル) ---> 位置ハンドル
374
375 .. index:: 
376         pair: ファイル; Fsetpos()
377
378 Fsetpos() 関数
379 ==============
380
381 Fsetpos() 関数は現在のファイルの位置を設定します。
382
383 文法:
384
385 .. code-block:: ring
386
387         Fsetpos(ファイルハンドル,位置ハンドル)
388
389 .. index:: 
390         pair: ファイル; Clearerr()
391
392 Clearerr() 関数
393 ===============
394
395 clearerr () 関数は EOF エラーとエラーインジケーターをストリームから消去します。
396
397 文法:
398
399 .. code-block:: ring
400
401         Clearerr(ファイルハンドル)
402
403 .. index:: 
404         pair: ファイル; Feof()
405
406 Feof() 関数
407 ===========
408
409 Feof() 関数は EOF インジケーターをテストします。
410
411 文法:
412
413 .. code-block:: ring
414
415         Feof(ファイルハンドル) ---> EOF ならば 1 を、そうでなければ 0 を返します。
416
417 .. index:: 
418         pair: ファイル; Ferror()
419
420 Ferror() 関数
421 =============
422
423 Ferror() 関数 はエラーインジケーターをテストします。
424
425 文法:
426
427 .. code-block:: ring
428
429         Ferror(ファイルハンドル) ---> エラーならば 1 を、そうでなければ 0 を返します。
430
431 .. index:: 
432         pair: ファイル; Perror()
433
434 Perror() 関数
435 =============
436
437 Perror() 関数は標準出力エラーへエラーメッセージを表示します。
438
439 文法:
440
441 .. code-block:: ring
442
443         Perror(cErrorMessage)
444
445 .. index:: 
446         pair: ファイル; Fgetc()
447
448 Fgetc() 関数
449 ============
450
451 Fgetc() 関数はストリームから次の文字を取得します。
452
453 文法:
454
455 .. code-block:: ring
456
457         Fgetc(ファイルハンドル) ---> 文字または EOF を返します。
458
459
460 .. index:: 
461         pair: ファイル; Fgets()
462
463 Fgets() 関数
464 ============
465
466 Fgets() 関数はストリームから新しい行を読み取ります。
467
468 文法:
469
470 .. code-block:: ring
471
472         Fgets(ファイルハンドル,nSize) ---> 文字列
473
474 この関数は nSize 文字まで読み取る、または新しい行を読み取るか EOF になると停止します。
475
476 .. index:: 
477         pair: ファイル; Fputc()
478
479 Fputc() 関数
480 ============
481
482 Fputc() 関数は文字をストリームへ書き込みます。
483
484 文法:
485
486 .. code-block:: ring
487
488         Fputc(ファイルハンドル,cChar)
489
490 .. index:: 
491         pair: ファイル; Fputs()
492
493 Fputs() 関数
494 ============
495
496 Fputs() 関数は文字列をストリームへ書き込みます。
497
498 文法:
499
500 .. code-block:: ring
501
502         Fputs(ファイルハンドル,cString)
503
504
505 .. index:: 
506         pair: ファイル; Ungetc()
507
508 Ungetc() 関数
509 =============
510
511 Ungetc() 関数は文字をストリームへプッシュします。
512
513 文字は次回の読み取りから利用できます。
514
515 文法:
516
517 .. code-block:: ring
518
519         Ungetc(ファイルハンドル,文字)
520
521
522 .. index:: 
523         pair: ファイル; Fread()
524
525 Fread() 関数
526 ============
527
528 Fread() 関数はデータをストリームへ読み込みます。 
529
530 文法:
531
532 .. code-block:: ring
533
534         Fread(ファイルハンドル,nSize)
535
536 .. index:: 
537         pair: ファイル; Fwrite()
538
539 Fwrite() 関数
540 =============
541
542 Fwrite() 関数はデータをストリームへ書き込みます。
543
544 文法:
545
546 .. code-block:: ring
547
548         Fwrite(ファイルハンドル,cString)
549
550
551 .. index:: 
552         pair: ファイル; Fexists()
553
554 Fexists() 関数
555 ==============
556
557 Fexists() 関数はファイルの存在を確認します。
558
559 文法:
560
561 .. code-block:: ring
562
563         Fexists(cFileName) ---> ファイルが存在する場合は 1 を返します。
564
565 用例:
566
567 .. code-block:: ring
568
569         see fexists("b:\mahmoud\apps\ring\ring.exe") + nl +
570             fexists("b:\mahmoud\apps\ring\ring2.exe") + nl 
571
572 実行結果:
573
574 .. code-block:: ring
575
576         1
577         0
578
579 .. index:: 
580         pair: ファイル; 用例
581
582 用例
583 ====
584
585 このプログラムはファイル関数をテストします。
586
587 .. code-block:: ring
588
589         See "testing file functions" + nl
590
591         See "open file" + nl
592         fp = fopen(exefolder() + "../tests/scripts/s65.ring","r")
593
594         See "reopen" + nl
595         fp = freopen(exefolder() + "../tests/scripts/s78.ring","r",fp)
596         See "close file" + nl
597         fclose(fp)
598
599         see "temp file" + nl
600         fp = tempfile()
601         fclose(fp)
602
603         see "temp name" + nl
604         see tempname() + nl
605
606         remove(exefolder() + "../tests/scripts/mytest2.txt")
607         write(exefolder() + "../tests/scripts/tests1.txt","hello")
608         rename(exefolder() + "../tests/scripts/test1.txt",exefolder() +
609                                                 "../tests/scripts/mytests2.txt")
610
611         see "print file" + nl
612         fp = fopen(exefolder() + "../samples/fromdoc/filefuncs.ring","r")
613         r = fgetc(fp)
614         while isstring(r)
615                         see r
616                         r = fgetc(fp)
617         end
618         fclose(fp)
619
620         see nl+"print line from the file" + nl
621         fp = fopen(exefolder() + "../samples/fromdoc/filefuncs.ring","r")
622         r = fgets(fp,33)
623         see r + nl
624         fclose(fp)
625         fp = fopen(exefolder() + "../tests/scripts/test78.txt","w+")
626         fseek(fp,0,2) # ファイルの末尾へ移動
627         fputc(fp,"t")
628         fputc(fp,"e")
629         fputc(fp,"s")
630         fputc(fp,"t")
631         fputs(fp,"tests2")
632         fclose(fp)
633
634         see "print file" + nl
635         see read(exefolder() + "../tests/scripts/test78.txt")
636
637         fp = fopen(exefolder() + "../tests/scripts/test78.txt","r")
638         see "testing ungetc() " + nl
639         for x = 1 to 3
640                         r = fgetc(fp)
641                         see r + nl
642                         ungetc(fp,r)
643         next
644         fclose(fp)
645
646         see "testing fread() " + nl
647         fp = fopen(exefilename(),"rb")
648         r = fread(fp,100)
649         see r + nl
650         fclose(fp)
651
652         see "testing fwrite() " + nl
653         fp = fopen(exefolder() + "../tests/scripts/test1.txt","wb")
654         fwrite(fp,r)
655         fclose(fp)
656
657 この用例はバイナリファイルの内容を表示します。
658
659 .. code-block:: ring
660
661         see "Testing: fread()" +" FileName: "+ exefilename() +nl +nl
662         fp = fopen(exefilename(),"rb")
663         r = fread(fp,800)
664         for n =1 to len(r)
665                 if isprint(substr(r, n, 1)) 
666                         see substr(r, n, 1) 
667                 else
668                         see "." 
669                 ok
670                 ### 1行あたり 80 文字
671                 if n % 80 = 0  
672                         see nl
673                 ok
674         next
675         fclose(fp)
676
677 .. index:: 
678         pair: ファイル; 数値とバイト
679
680 数値とバイト
681 ============
682
683 この関数は数値とバイトとの間で変換を行います。
684
685 * Int2Bytes()
686 * Float2Bytes()
687 * Double2Bytes()
688 * Bytes2Int()
689 * Bytes2Float()
690 * Bytes2Double()
691
692 用例:
693
694 .. code-block:: ring
695
696         see "Test Int2Bytes() and Bytes2Int() - Value : 77" + nl
697         r = Int2Bytes(77)
698         see "Int Size : " + len(r) + nl
699         see r + nl
700         see Bytes2Int(r) + nl
701         see "Test Float2Bytes() and Bytes2Float() - Value 77.12" + nl
702         r = Float2Bytes(77.12)
703         see "Float Size : " + len(r) + nl
704         see r + nl
705         see Bytes2Float(r) + nl
706         see "Test Double2Bytes() and Bytes2Double() - Value 9999977.12345" + nl
707         r = Double2Bytes(9999977.12345)
708         see "Double Size : " + len(r) + nl
709         see r + nl
710         decimals(5)
711         see Bytes2Double(r) + nl