FileOpen [AHK_L 42+]

ÆÄÀÏÀ» ¿±´Ï´Ù.

file := FileOpen(Filename, Flags [, Encoding])

¸Å°³º¯¼ö

Filename

¿­ ÆÄÀÏÀÇ °æ·Î. Àý´ë °æ·Î¸¦ ÁöÁ¤ÇÏÁö ¾ÊÀ¸¸é A_WorkingDir¿¡ ÀÖ´Ù°í °£Áֵ˴ϴÙ.

[v1.1.17+]: Ç¥ÁØ ÀÔ·Â/Ãâ·Â/¿¡·¯ ½ºÆ®¸²À» ¿­·Á¸é º°Ç¥¸¦ (¶Ç´Â º°Ç¥ µÎ °³¸¦) ¾Æ·¡¿¡ º¸¿©ÁÖ´Â ¹Ù¿Í °°ÀÌ ÁöÁ¤ÇϽʽÿÀ:

FileOpen("*", "r")   ; Ç¥ÁØÀÔ·Â
FileOpen("*", "w")   ; Ç¥ÁØÃâ·Â
FileOpen("**", "w")  ; Ç¥ÁØ¿¡·¯
Flags

[in AHK_L 54+] ¿øÇÏ´Â Á¢±Ù ¸ðµå¸¦ °¡¸®Å°´Â ¹®ÀÚ ±×¸®°í ´ÙÀ½¿¡ ´Ù¸¥ ¿É¼ÇÀÌ µû¶ó¿Í ±¸¼ºµÈ ¹®ÀÚ¿­ (¼±ÅÃÀûÀ¸·Î »çÀÌ¿¡ ½ºÆäÀ̽º ¶Ç´Â ÅÇ); ¶Ç´Â [ AHK_L 42+] ¼ýÄ¡ Ç÷¡±×ÀÇ Á¶ÇÕ (ÇÕ°è). Áö¿øµÇ´Â °ªµéÀº ¾Æ·¡ Å×ÀÌºí¿¡ ±â¼úÇÕ´Ï´Ù.

Encoding

ÆÄÀÏ¿¡ UTF-8 ¶Ç´Â UTF-16 ¹ÙÀÌÆ® ¼ø¼­ Ç¥½ÄÀÌ ¾øÀ» ¶§, ¶Ç´Â h (ÇÚµé) ÇÁ·¡±×°¡ »ç¿ëµÉ ¶§ ÅؽºÆ® I/O¿¡ »ç¿ëÇÒ ÄÚµå ÆäÀÌÁöÀÔ´Ï´Ù. »ý·«µÇ¸é, A_FileEncodingÀÇ ÇöÀç °ªÀÌ »ç¿ëµË´Ï´Ù.

Flags

  Á¢±Ù ¸ðµå (¼­·Î-¹èŸÀûÀÓ)
r0Read: ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é ½ÇÆÐÇÕ´Ï´Ù.
w1Write: »õ ÆÄÀÏÀ» »ý¼ºÇÕ´Ï´Ù, ±âÁ¸ÀÇ ÆÄÀÏÀ» µ¤¾î¾²±â ÇÕ´Ï´Ù.
a2Append: ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é »õ ÆÄÀÏÀ» »ý¼ºÇÕ´Ï´Ù, ±×·¸Áö ¾ÊÀ¸¸é ÆÄÀÏ Æ÷ÀÎÅ͸¦ ÆÄÀÏÀÇ ³¡À¸·Î À̵¿ÇÕ´Ï´Ù.
rw3Read/Write: ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é »õ ÆÄÀÏÀ» »ý¼ºÇÕ´Ï´Ù.
h FilenameÀÌ °´Ã¼ ¾È¿¡ ½Î ³ÖÀ» ÆÄÀÏ ÇÚµéÀÓÀ» ³ªÅ¸³À´Ï´Ù. °øÀ¯ ¸ðµå Ç÷¡±×´Â ¹«½ÃµË´Ï´Ù. ±×¸®°í ÀÌ ÇÚµéÀÌ °¡¸®Å°´Â ÆÄÀÏÀ̳ª ½ºÆ®¸²Àº ¹ÙÀÌÆ® ¼ø¼­ Ç¥½ÄÀÌ Á¡°ËµÇÁö ¾Ê½À´Ï´Ù. ÆÄÀÏ ÇÚµéÀº ÆÄÀÏ °´Ã¼°¡ Æı«µÉ ¶§ ÀÚµ¿À¸·Î Àá±âÁö ¾Ê½À´Ï´Ù. ±×¸®°í Close¸¦ È£ÃâÇØ ºÁ¾ß ¾Æ¹« È¿°úµµ ¾ø½À´Ï´Ù. ÁÖÀÇ ÇϽʽÿÀ. FilenameÀÌ ÆÄÀÌÇÁ³ª Åë½Å ÀåÄ¡ °°ÀÌ À§Ä¡ ÁöÁ¤ÀÌ ¾È µÇ´Â(nonseeking) ÀåÄ¡¸¦ °¡¸®Å°´Â ÇÚµéÀ̸é Seek, Tell ±×¸®°í Length¸¦ »ç¿ëÇÏ¸é ¾È µË´Ï´Ù.
  °øÀ¯ ¸ðµå Ç÷¡±×
-rwd Àбâ, ¾²±â ±×¸®°í Áö¿ì±â Á¢±ÙÀ» À§ÇØ ÆÄÀÏÀ» Àá±Ý´Ï´Ù. r, w ±×¸®°í dÀ» Á¶ÇÕÇØ »ç¿ëÇصµ µË´Ï´Ù. -¸¦ ÁöÁ¤Çϸé -rwd¸¦ ÁöÁ¤ÇÑ °Í°ú °°½À´Ï´Ù. ¿ÏÀüÈ÷ »ý·«Çϸé, ±âº»Àº ¸ðµç Á¢±ÙÀ» °øÀ¯ÇÏ´Â °ÍÀÔ´Ï´Ù.
 0Flags°¡ ¼ýÄ¡À̸é, °øÀ¯ ¸ðµå Ç÷¡±×°¡ ¾øÀ» °æ¿ì ÆÄÀÏÀÌ Àá±é´Ï´Ù.
 0x100read Á¢±ÙÀ» °øÀ¯ÇÕ´Ï´Ù.
 0x200write Á¢±ÙÀ» °øÀ¯ÇÕ´Ï´Ù.
 0x400delete Á¢±ÙÀ» °øÀ¯ÇÕ´Ï´Ù.
  ÁÙ ³¡(End of line (EOL)) ¿É¼Ç
`n4ÀÐÀ» ¶§ `r`nÀ» `nÀ¸·Î ±³Ã¼ÇÕ´Ï´Ù. ±×¸®°í ¾µ ¶§ `nÀ» `r`nÀ¸·Î ±³Ã¼ÇÕ´Ï´Ù.
`r8ÀÐÀ» ¶§ ³ªÈ¦·Î `rÀ» `nÀ¸·Î ±³Ã¼ÇÕ´Ï´Ù.

¹Ýȯ °ª

ÆÄÀÏÀÌ ¼º°øÀûÀ¸·Î ¿­¸®¸é, ¹Ýȯ °ªÀº ÆÄÀÏ °´Ã¼ÀÔ´Ï´Ù.

ÇÔ¼ö°¡ ½ÇÆÐÇϸé, ¹Ýȯ °ªÀº 0ÀÌ°í [in AHK_L 54+] A_LastError´Â ¿¡·¯ Äڵ尡 ´ã±é´Ï´Ù.

ÇÔ¼ö°¡ ¼º°øÇß´ÂÁö Á¡°ËÇÏ·Á¸é if file ¶Ç´Â IsObject(file)¸¦ »ç¿ëÇϽʽÿÀ.

³íÆò

UTF-8 ¶Ç´Â UTF-16 ÆÄÀÏÀÌ »ý¼ºµÉ ¶§, ¹ÙÀÌÆ® ¼ø¼­ Ç¥½ÄÀÌ ÆÄÀÏ¿¡ ¾º¿©Áý´Ï´Ù. ´Ü, Encoding (¶Ç´Â EncodingÀ» »ý·«Çϸé A_FileEncoding)¿¡ "UTF-8-RAW" ¶Ç´Â "UTF-16-RAW"ÀÌ ´ã°Ü ÀÖ´Â °æ¿ì´Â ¿¹¿ÜÀÔ´Ï´Ù.

UTF-8 ¶Ç´Â UTF-16 ¹ÙÀÌÆ® ¼ø¼­ Ç¥½Ä(byte order mark (BOM))À» ´ã°í ÀÖ´Â ÆÄÀÏÀÌ Àбâ Á¢±ÙÀ» À§ÇØ ¿­¸± ¶§, BOMÀº ÆÄÀÏ Æ÷ÀÎÅ͸¦ ±× ´ÙÀ½À¸·Î À̵¿Çؼ­ Ãâ·ÂÀ¸·ÎºÎÅÍ Á¦¿ÜµË´Ï´Ù. ±×·¯¹Ç·Î, File.PositionÀº ÆÄÀÏÀ» ¿­°í ³­ÈÄ Áï½Ã 3 ¶Ç´Â 2¸¦ º¸°íÇÕ´Ï´Ù.

°ü·Ã Ç׸ñ

FileEncoding, File Object, FileRead

¿¹Á¦

; ¿¹Á¦: ´ÙÀ½ ½ºÅ©¸³Æ®´Â ÅؽºÆ®¸¦ ÆÄÀÏ¿¡ ¾´ ´ÙÀ½ ´Ù½Ã ¸Þ¸ð¸®·Î ÀÐ¾î µéÀÔ´Ï´Ù.
; ´ÙÀ½ DllCall-¿¹Á¦¿Í ±â´ÉÀÌ °°½À´Ï´Ù.

FileSelectFile, FileName, S16,, Create a new file:
if (FileName = "")
	return
file := FileOpen(FileName, "w")
if !IsObject(file)
{
	MsgBox Can't open "%FileName%" for writing.
	return
}
TestString := "This is a test string.`r`n"  ; ÆÄÀÏÀ» ÀÌ·± ½ÄÀ¸·Î ¾µ ¶§,  »õ ÁÙÀ» ½ÃÀÛÇÏ·Á¸é `n ¸»°í `r`nÀ» »ç¿ëÇϽʽÿÀ.
file.Write(TestString)
file.Close()

; ÀÌÁ¦ ÆÄÀÌÀ» ´Ù ½èÀ¸¹Ç·Î, ´Ù½Ã ¸Þ¸ð¸®·Î ±× ³»¿ëÀ» ÀÐ¾î µéÀÔ´Ï´Ù.
file := FileOpen(FileName, "r-d") ; ÆÄÀÏÀ» ÀнÀ´Ï´Ù ("r"),  Áö¿ì±â ("-d")¸¸ »©°í ¸ðµç Á¢±ÙÀ» °øÀ¯ÇÕ´Ï´Ù.
if !IsObject(file)
{
	MsgBox Can't open "%FileName%" for reading.
	return
}
CharsToRead := StrLen(TestString)
TestString := file.Read(CharsToRead)
file.Close()
MsgBox The following string was read from the file: %TestString%
; Àбâ Àü¿ë ¸ðµå·Î ½ºÅ©¸³Æ®¸¦ ¿±´Ï´Ù. ±×¸®°í ±×ÀÇ Ã¹ ÁÙÀ» ÀнÀ´Ï´Ù:
file := FileOpen(A_ScriptFullPath, "r")
MsgBox % file.ReadLine()

; ´ÙÀ½ µ¥¸ð¸¦ º¸¿©ÁÖ±â À§ÇØ ÄÜ¼Ö Ã¢À» ¿±´Ï´Ù:
DllCall("AllocConsole")
; ¾îÇø®ÄÉÀ̼ÇÀÇ stdin/stdout ½ºÆ®¸²À» »õÁÙ-º¯È¯ ¸ðµå·Î ¿±´Ï´Ù.
stdin  := FileOpen("*", "r `n")  ; Requires v1.1.17+
stdout := FileOpen("*", "w `n")
; ¿¹Àü ¹öÀü¿¡´Â ´ÙÀ½À» »ç¿ëÇÕ´Ï´Ù:
;   stdin  := FileOpen(DllCall("GetStdHandle", "int", -10, "ptr"), "h `n")
;   stdout := FileOpen(DllCall("GetStdHandle", "int", -11, "ptr"), "h `n")
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