跳轉到內容

環/課程/檔案

來自華夏公益教科書,開放的書籍,開放的世界
<

本章我們將學習關於檔案函式。

  • Read()
  • Write()
  • Dir()
  • Rename()
  • Remove()
  • fopen()
  • fclose()
  • fflush()
  • freopen()
  • tempfile()
  • tempname()
  • fseek()
  • ftell()
  • rewind()
  • fgetpos()
  • fsetpos()
  • clearerr()
  • feof()
  • ferror()
  • perror()
  • fgetc()
  • fgets()
  • fputc()
  • fputs()
  • ungetc()
  • fread()
  • fwrite()
  • fexists()



Read() 函式

[編輯 | 編輯原始碼]

我們可以使用 Read() 函式讀取檔案內容。

語法

	Read(cFileName) ---> String contains the file content

示例

	see read("myfile.txt")

read 函式也可以讀取二進位制檔案。

示例

	see read("myapp.exe")



Write() 函式

[編輯 | 編輯原始碼]

我們可以使用 Write() 函式將字串寫入檔案。

write 函式可以將二進位制資料寫入二進位制檔案。

語法

	Write(cFileName,cString)	# write string cString to file cFileName

示例

	# copy file
	cFile = read("ring.exe")
	write("ring2.exe",cFile)



Dir() 函式

[編輯 | 編輯原始碼]

我們可以使用 Dir() 函式獲取資料夾內容(檔案和子資料夾)。

語法

	Dir(cFolderPath) ---> List contains files & sub folders.

此函式返回一個列表,每個列表項是一個包含兩個項的列表

  • 檔案/子資料夾名稱
  • 型別(0 = 檔案,1 = 資料夾/目錄)

示例

	see "Testing DIR() " + nl
	mylist = dir("C:\myfolder")
	for x in mylist
		if x[2] 
			see "Directory : " + x[1] + nl
		else
			see "File : " + x[1] + nl
		ok
	next
	see "Files count : " + len(mylist)


Rename() 函式

[編輯 | 編輯原始碼]

我們可以使用 Rename() 函式重新命名檔案。

語法

	Rename(cOldFileName,cNewFileName)

示例

	rename("file.txt","help.txt")

Remove() 函式

[編輯 | 編輯原始碼]

我們可以使用 Remove() 函式刪除檔案。


語法

	Remove(cFileName)

示例

	remove("test.txt")

Fopen() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fopen() 函式開啟檔案。

語法

	Fopen(cFileName,cMode) ---> File Handle
=====	==========================================
Mode	Description
=====	==========================================
"r"     Reading (The file must exist)
"w"     Writing (create empty file / overwrite)
"a"     Appends (create file if it doesn't exist)
"r+"    update (reading/writing)
"w+"    Create empty file (reading/writing)
"a+"    reading & appending
=====	==========================================

Fclose() 函式

[編輯 | 編輯原始碼]

當我們使用 fopen() 函式開啟檔案時,我們可以使用 Fclose() 函式關閉它。

語法

	Fclose(file handle)

Fflush() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fflush() 函式清空流的輸出緩衝區。

語法

	Fflush(file handle)

Freopen() 函式

[編輯 | 編輯原始碼]

我們可以使用相同的控制代碼開啟另一個檔案,並在關閉舊檔案的同時開啟另一個檔案。

語法

	Freopen(cFileName,cMode,file handle) ---> file handle

示例

	freopen("myprogoutput.txt","w+",stdout)
	see "welcome" + nl
	for x = 1 to 10
		see x + nl
	next

/* ** 閱讀 : https://en.wikipedia.org/wiki/Device_file#Device_files ** 下面的程式碼不可移植,我們可以在使用之前使用 iswindows() ** 併為每個作業系統編寫特殊的程式碼。 */

freopen("CON","w",stdout) # 對於 Microsoft Windows 請參閱 "Done" + nl # 再次列印到 stdout

輸出

	# Output to stdout
	Done

# 輸出到檔案 : myprogoutput.txt welcome 1 2 3 4 5 6 7 8 9 10

Tempfile() 函式

[編輯 | 編輯原始碼]

Tempfile() 函式建立一個臨時檔案(二進位制)。

當流關閉時,該檔案將自動刪除。

語法

	TempFile() ---> file handle


Tempname() 函式

[編輯 | 編輯原始碼]

我們可以使用 Tempname() 函式生成臨時檔名。

生成的名稱將與任何現有檔案的名稱不同。

語法

	Tempname() ---> generated file name as string

Fseek() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fseek() 函式設定流的檔案位置。

語法

	Fseek(file handle, nOffset, nWhence) ---> zero if successful

下表顯示了 nWhence 值。

=====	===============================
Value	Description
=====	===============================
0       Beginning of file
1       Current position
2       End of file
=====	===============================

Ftell() 函式

[編輯 | 編輯原始碼]

我們可以使用 Ftell() 函數了解流的當前檔案位置。

語法

	Ftell(file handle) ---> file position as number

Rewind() 函式

[編輯 | 編輯原始碼]

我們可以使用 Rewind() 函式將檔案位置設定為檔案開頭。

語法

	
	Rewind(file handle)

Fgetpos() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fgetpos() 函式獲取對當前檔案位置的控制代碼。

語法

	Fgetpos(file handle) ---> position handle

Fsetpos() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fgetpos() 函式設定當前檔案位置。

語法

	Fsetpos(file handle,position handle)

Clearerr() 函式

[編輯 | 編輯原始碼]

我們可以使用 clearerr() 函式清除流的 EOF 錯誤和錯誤指示符。

語法

	Clearerr(file handle)

Feof() 函式

[編輯 | 編輯原始碼]

我們可以使用 Feof() 函式測試檔案結束指示符。

語法

	Feof(file handle) ---> returns 1 if EOF and 0 if not

Ferror() 函式

[編輯 | 編輯原始碼]

我們可以使用 Ferror() 函式測試給定流的錯誤指示符。

語法

	Ferror(file handle) ---> returns 1 if error and 0 if not

Perror() 函式

[編輯 | 編輯原始碼]

我們可以使用 Perror() 函式將錯誤訊息列印到 stderr。

語法

	Perror(cErrorMessage)

Fgetc() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fgetc() 函式從流中獲取下一個字元。

語法

	Fgetc(file handle) ---> returns character or EOF


Fgets() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fgets() 函式從流中讀取新行。

語法

	Fgets(file handle,nSize) ---> string

該函式在讀取了 nSize 個字元、讀取了新行字元或 EOF 時停止。

Fputc() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fputc() 函式將一個字元寫入流。

語法

	Fputc(file handle,cChar)

Fputs() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fputs() 函式將一個字串寫入流。

語法

	Fputs(file handle,cString)


Ungetc() 函式

[編輯 | 編輯原始碼]

我們可以使用 Ungetc() 函式將一個字元推送到流中。

該字元將在下次讀取時可用。

語法

	Ungetc(file handle,character)


Fread() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fread() 函式從流中讀取資料。

語法

	Fread(file handle,nSize)

Fwrite() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fwrite() 函式將資料寫入流。

語法

	Fwrite(file handle,cString)


Fexists() 函式

[編輯 | 編輯原始碼]

我們可以使用 Fexists() 函式檢查檔案是否存在。

語法

	Fexists(cFileName) ---> returns 1 if the file exists

示例

	see fexists("b:\mahmoud\apps\ring\ring.exe") + nl +
	    fexists("b:\mahmoud\apps\ring\ring2.exe") + nl

輸出

	1
	0

以下程式測試了一些檔案函式。

	See "Testing file functions" + nl

	See "open file" + nl
	fp = fopen("tests\s65.ring","r")

	See "reopen" + nl
	fp = freopen("tests\s78.ring","r",fp)
	See "close file" + nl
	fclose(fp)

	see "temp file" + nl
	fp = tempfile()
	fclose(fp)

	see "temp name" + nl
	see tempname() + nl

	remove("tests\mytest1.txt")
	write("tests\test1.txt","hello")
	rename("tests\test1.txt","tests\mytest2.txt")

	see "print file" + nl
	fp = fopen("tests\file.ring","r")
	r = fgetc(fp)
	while isstring(r)
		see r
		r = fgetc(fp)
	end
	fclose(fp)

	see nl+"print line from the file" + nl
	fp = fopen("tests\file.ring","r")
	r = fgets(fp,100)
	see r
	fclose(fp)

	fp = fopen("tests\myfile.txt","rw+")
	fseek(fp,0,2) # goto end of file
	fputc(fp,"t")
	fputc(fp,"e")
	fputc(fp,"s")
	fputc(fp,"t")
	fputs(fp,"test2")
	fclose(fp)

	see "print file" + nl
	see read("tests\myfile.txt")

	fp = fopen("tests\myfile.txt","r")
	see "testing ungetc() " + nl
	for x = 1 to 3
		r = fgetc(fp)
		see r + nl
		ungetc(fp,r)
	next
	fclose(fp)

	see "testing fread() " + nl
	fp = fopen("ring.exe","r")
	r = fread(fp,100)
	see r + nl
	fclose(fp)

	see "testing fwrite() " + nl
	fp = fopen("tests\myfile.txt","wb")
	fwrite(fp,r)
	fclose(fp)


華夏公益教科書