FileOpen

ファイルを開いて、特定のコンテンツを読み込んだり、新しいコンテンツを書き込んだりします。

FileObj := FileOpen(Filename, Flags , Encoding)

パラメータ

Filename

型:文字列

開くファイルのパス。絶対パスが指定されていない場合は、A_WorkingDirにあると仮定されます。

以下のようにアスタリスク(または2つ)を指定すると、標準入力/出力/エラーストリームを開くことができます:

FileOpen("*", "r")   ; 標準入力用
FileOpen("*", "w")   ; 標準出力用
FileOpen("**", "w")  ; 標準エラー用
Flags

型:文字列または整数

希望するアクセスモードを示す文字列の後に他のオプション(オプションで空白またはタブを挟む)が続くか、または数値フラグの組み合わせ(合計)です。サポートされる値は、以下の表のとおりです。

エンコード

型:文字列または整数

If omitted, the default encoding (as set by FileEncoding or CP0 otherwise) will be used. If blank, it defaults to CP0 (the system default ANSI code page). Otherwise, specify the encoding or code page to use for text I/O, e.g. "UTF-8", "UTF-16", "CP936" or 936.

If the file contains a UTF-8 or UTF-16 byte order mark (BOM), or if the h (handle) flag is used, this parameter and the default encoding will be ignored, unless the file is being opened with write-only access (i.e. the previous contents of the file are being discarded).

Flags

アクセスモード(相互排他的)

Flag Dec Hex 説明
r 0 0x0 Read:ファイルが存在しない場合、失敗します。
w 1 0x1 Write:新しいファイルを作成し、既存のファイルを上書きします。
a 2 0x2 Append:ファイルが存在しない場合は新しいファイルを作成し、それ以外の場合はファイルポインタをファイルの末尾に移動します。
rw 3 0x3 Read/Write:ファイルが存在しなかった場合、新しいファイルを作成します。
h     Filenameがオブジェクトにラップするためのファイルハンドルであることを示します。共有モードフラグは無視され、ハンドルが表すファイルまたはストリームはバイトオーダーマークをチェックしない。The file handle is not closed automatically when the file object is destroyed and calling File.Close has no effect. Note that File.Seek, File.Pos and File.Length should not be used if Filename is a handle to a nonseeking device such as a pipe or a communications device.

Sharing mode flags

Flag Dec Hex 説明
-rwd     読み取り、書き込み、および削除のアクセスに対してファイルをロックします。rwdの組み合わせは自由です。-を指定することは、-rwdを指定することと同じです。完全に省略された場合、デフォルトはすべてのアクセスを共有します。
  0 0x0 Flagsが数値の場合、共有モードフラグがない場合は、ファイルがロックされます。
  256 0x100 読み取りアクセスを共有します。
  512 0x200 書き込みアクセスを共有します。
  1024 0x400 削除アクセスを共有します。

End of line (EOL) options

Flag Dec Hex 説明
`n 4 0x4 読むときは`r`n`nに、書くときは`n`r`nに置き換えてください。
`r 8 0x8 読み取り時にスタンドアロンの`r`nに置き換える。

戻り値

型:Object

戻り値は、ファイルへのオープンハンドルをカプセル化した新しいFileオブジェクトです。このオブジェクトのメソッドとプロパティを使用して、ファイルのコンテンツにアクセスします。

エラー

ファイルを開くことができない場合は、OSErrorが投げられる。

備考

File.ReadLineは、`n`r`n`rを常に行末としてサポートし、`rまたは`nオプションが使用されているかどうかにかかわらず、その戻り値にそれらを含めないようにします。このオプションは、File.Readによって返されたテキスト、File.WriteまたはFile.WriteLineによって書き込まれたテキスト内の行末の変換にのみ影響します。

When a UTF-8 or UTF-16 file is created, a byte order mark (BOM) is written to the file unless Encoding or the default encoding (as set by FileEncoding) is "UTF-8-RAW" or "UTF-16-RAW".

UTF-8またはUTF-16のバイトオーダーマーク(BOM)を含むファイルを読み込みアクセスで開いた場合、ファイルポインタをBOMの後に配置することで出力から除外されるようにしました。Therefore, File.Pos may report 3 or 2 immediately after opening the file.

FileEncoding, File Object, FileRead

ファイルにテキストを書き込み、それをメモリに読み戻します(このDllCallの例と同じ機能を提供します)。

FileName := FileSelect("S16",, "Create a new file:")
if (FileName = "")
    return
try
    FileObj := FileOpen(FileName, "w")
catch as Err
{
    MsgBox "Can't open '" FileName "' for writing."
        . "`n`n" Type(Err) ":" Err.Message
    return
}
TestString := "This is a test string.`r`n"  ; この方法でファイルを書く場合、改行には `n ではなく `r`n を使用します。
FileObj.Write(TestString)
FileObj.Close()

; ファイルが書き込まれたので、その内容をメモリに読み返してください。
try
    FileObj := FileOpen(FileName, "r-d") ; read the file ("r")は、削除("-d")を除くすべてのアクセスを共有します。
catch as Err
{
    MsgBox "Can't open '" FileName "' for reading."
        . "`n`n" Type(Err) ":" Err.Message
    return
}
CharsToRead := StrLen(TestString)
TestString := FileObj.Read(CharsToRead)
FileObj.Close()
MsgBox "The following string was read from the file:" TestString

スクリプトを読み取り専用で開き、その1行目を読み取る。

Script := FileOpen(A_ScriptFullPath, "r")
MsgBox Script.ReadLine()

標準入出力ストリームの使い方を説明します。

; このデモのためにコンソールウィンドウを開いてください:
DllCall("AllocConsole")
; アプリケーションのstdin/stdoutストリームをオープンします。
stdin  := FileOpen("*", "r")
stdout := FileOpen("*", "w")
stdout.Write("Enter your query.`n\> ")
stdout.Read(0) ; 書き込みバッファをフラッシュします。
query := RTrim(stdin.ReadLine(), "`n")
stdout.WriteLine("Your query was '" query "'. Have a nice day.")
stdout.Read(0) ; 書き込みバッファをフラッシュします。
Sleep 5000