七、文件操作函数

七、文件操作函数

文件操作函数完成文件的输入、修改、输出、有效性检查、删除以及确定文件长度等功能。在访问文件中的数据之前,需要首先打开文件。文件用毕之后,需要关闭文件。文件的打开方式有两种:行模式(Line Mode)和流模式(Stream Mode)。

在行模式下,读取数据时,一次写入一行,直到读取到回车(CR)换行(LF)字符或文件的结束符时停止;写文件时,一次写入一行,在每行的末尾自动添加上回车换行字符(具体添加的字符与操作系统有关)。

在流模式下,读取数据时,按字节一次读取,直到文件结束符(EOF)时才算读完文件,写数据时,按数据原样写入文件,并不另加其他任何控制字符(包括回车换行符)。

   

1、FileClose()

功  能:关闭先前用FileOpen()函数打开的文件。

语  法:FileClose ( fileno )

参  数:fileno:integer,指定要关闭文件的文件句柄,该句柄使用FileOpen()函数打开文件时得到。

返回值:Integer。函数执行成功时返回1,发生错误时返回-1。如果fileno参数的值为NULL,那么

FileClose()函数返回NULL。

示  例:These statements open and then close the file EMPLOYEE.DAT. The variable li_FileNum stores the number assigned to the file when FileOpen opens the file. FileClose uses that number to close the file:

integer li_FileNum

li_FileNum = FileOpen("EMPLOYEE.DAT")

. . . // Some processing

FileClose(li_FileNum)

 

2、FileDelete()

功  能:删除指定的文件。

语  法:FileDelete ( filename )

参  数:filename:string类型,指定要删除文件的文件名,其中可以包含路径。

返回值:Boolean。函数执行成功时返回TRUE,发生错误时返回FALSE。如果filename参数的值为NULL,那么FileDelete()函数返回NULL。

示  例:These statements delete the file the user selected in the Open File window:

integer ret, value

string docname, named

value = GetFileOpenName("Select File,"docname, named, "DOC","Doc Files (*.DOC),*.DOC")

IF value = 1 THEN ret = MessageBox("Delete","Delete file?", Question!, OKCancel!)

IF ret = 1 THEN FileDelete(docname)

 

3、FileExists()

功  能:检查指定的文件是否存在。

语  法:FileExists ( filename )

参  数:filename:string类型,指定要检查存在性的文件的文件名,其中可以包含路径。

返回值:Boolean。如果指定文件存在时返回TRUE,不存在时返回FALSE。如果filename参数的值为NULL,那么FileExists()函数返回NULL。

用  法:如果filename参数指定的文件被另一个应用加锁锁住,那么FileExists()函数也将返回FALSE。

示  例:This example determines if the file the user selected in the Save File window exists and, if so, asks the user if the file can be overwritten:

string ls_docname, ls_named

integer li_ret

boolean lb_exist

GetFileSaveName("Select File," ls_docname,ls_named, "pbl","Doc Files (*.DOC),*.DOC")

lb_exist = FileExists(ls_docname)

IF  lb_exist THEN

li_ret = MessageBox("Save","OK to write over" + ls_docname,Question!, YesNo!)

END IF

 

4、FileLength()

功  能:得到指定文件的长度(以字节为单位)。

语  法:FileLength ( filename )

参  数:filename:string类型,指定要得到其长度的文件的文件名,其中可以包含路径。

返回值:Long。函数执行成功时返回指定文件的长度(以字节为单位)。如果指定的文件不存在,函数返回-1。如果filename参数的值为NULL,那么FileLength()函数返回NULL。

示  例:This statement returns the length of the file EMPLOYEE.DAT in the current directory:

FileLength("EMPLOYEE.DAT")

These statements determine the length of the EMP.TXT file in the EAST directory and open the file:

long LengthA

integer li_FileNum

LengthA = FileLength("C:\EAST\EMP.TXT")

li_FileNum = FileOpen("C:\EAST\EMP.TXT", &

StreamMode!, Read!, LockReadWrite!)

The examples for FileRead illustrate reading files of different lengths.

 

5、FileOpen()

功  能:以指定的读写方式打开指定的文件,同时返回该文件的句柄。

语  法:FileOpen(filename{,filemode{,fileaccess{,filelock{,writemode,{creator,filetype}}}}})

参  数:filename:string类型,指定要打开文件的名称,其中可以包含路径;

filemode:FileMode枚举类型,可选项,指定文件打开方式。有效取值为:

LineMode! - 缺省值,行模式;

StreamMode! - 流模式。

fileaccess:FileAccess枚举类型,可选项,指定文件访问方式。有效取值为:

Read! - 缺省值,只读方式,这样打开的文件只能进行读操作;

Write! - 只写方式,这样打开的文件只能进行写操作。

filelock:FileLock枚举类型,可选项,指定文件加锁方式。有效取值为:

LockReadWrite! - 缺省值,只有打开该文件的用户能够访问该文件,其它用 户对该文件的访问均被拒绝;

LockRead! - 只有打开该文件的用户能够读该文件,但其它任何用户均可写该文件;

LockWrite! - 只有打开该文件的用户能够写该文件,但其它任何用户均可读该文件;

Shared! - 所有用户均可读写该文件。

writemode:WriteMode枚举类型,可选项,当fileaccess参数指定为Write!时,该参数指定在指定文件已经存在时数据的添加方式。有效取值为:

Append! - 缺省值,将数据添加到原文件尾部;

Replace! - 覆盖原有数据。

creator:可选项,用于Macintosh机,使用四个字符的字符串指定文件的创建者。指定该参数后,必须同时指定filetype参数。

filetype:可选项,用于Macintosh机,使用四个字符的字符串指定文件类型。

返回值:Integer。函数执行成功时返回打开文件的句柄,随后的文件操作函数利用该句柄完成对文件的操作。发生错误时函数返回-1。如果任何参数的值为NULL,那么FileOpen()函数返回NULL。

用法当文件以行模式打开时,每执行一次FileRead()函数读取一行数据;每执行一次FileWrite()函数,该函数自动在写出的字符串末尾增加一个回车(CR)换行(LF)符(这是应用程序在Windows 系统中运行时的情况,在UNIX下只加一个换行字符)。当文件以流模式打开时,执行一次FileRead()函数读取32,765个字节的数据,如果余下数据没有这么多,那么FileRead()函数就读取所有余下的数据;执行一次FileWrite()函数时,最多可写入32,765个字节的数据,并且不添加回车换行字符。

当文件以写方式使用FileOpen()函数打开时,如果指定的文件不存在,那么FileOpen()函数创建该文件。

示  例:This example uses the default arguments and opens the file EMPLOYEE.DAT for reading. The default settings are LineMode!, Read!, and LockReadWrite!. FileRead reads the file line by line and no other user is able to access the file until it is closed:

integer li_FileNum

li_FileNum = FileOpen("EMPLOYEE.DAT")

This example opens the file EMPLOYEE.DAT in the DEPT directory in stream mode (StreamMode!) for write only access (Write!). Existing data is overwritten (Replace!). No other users can write to the file (LockWrite!):

integer li_FileNum

li_FileNum = FileOpen("C:\DEPT\EMPLOYEE.DAT", &

StreamMode!, Write!, LockWrite!, Replace!)

 

6、FileRead()

功  能:从指定文件中读取数据。

语  法:FileRead ( fileno, variable )

参  数:fileno:integer类型,指定文件句柄(由FileOpen()函数得到);

variable:string或blob类型的变量,用于保存读取的数据。

返回值:Integer。函数执行成功时返回读取的字符数或字节数;如果在读取任何字符前读到了文件结束符(EOF),则FileRead()函数返回-100;当指定文件以行模式打开时,如果在读取任何字符之前遇到了回车(CR)或换行(LF)字符,则FileRead()函数返回0。如果发生其它错误,FileRead()函数返回-1。如果任何参数的值为NULL,那么FileRead()函数返回NULL。

用  法:当指定文件以行模式(Line Mode)打开时,FileRead()函数一次读取一行数据,并把它保存到参数variable中,然后跳过行结束符(回车换行符,操作系统不同,使用的字符也不同),把文件指针移动到下一行的起始位置。

当文件以流模式(Stream Mode)打开时,FileRead()函数或一直读取到文件结尾,或读取32,765字节的数据,决定于两者哪个数据长度更短些。

示  例:This example reads the file EMP_DATA.TXT if it is short enough to be read with one call to

FileRead:

integer li_FileNum

string ls_Emp_Input

long ll_FLength

ll_FLength = FileLength("C:\HR\EMP_DATA.TXT")

li_FileNum = FileOpen("C:\HR\EMP_DATA.TXT", StreamMode!)

IF ll_FLength < 32767 THEN

FileRead(li_FileNum, ls_Emp_Input)

END IF

 

This example reads the file EMP_PIC1.BMP and stores the data in the blob Emp_Id_Pic. The number of bytes read is stored in li_bytes:

integer li_fnum, li_bytes

blob Emp_Id_Pic

li_fnum = FileOpen("C:\HR\EMP_PIC1.BMP", StreamMode!)

li_bytes = FileRead(li_fnum, Emp_Id_Pic)

 

This example reads a file exceeding 32,765 bytes. After the script has read the file into the blob tot_b, you can call the SetPicture or String function to make use of the data, depending on the contents of the file:

integer li_FileNum, loops, i

long flen, bytes_read, new_pos

blob b, tot_b

// Set a wait cursor

SetPointer(HourGlass!)

// Get the file length, and open the file

flen = FileLength(sle_filename.Text)

li_FileNum = FileOpen(sle_filename.Text,StreamMode!, Read!, LockRead!)

// Determine how many times to call FileRead

IF flen > 32765 THEN

IF Mod(flen, 32765) = 0 THEN

loops = flen/32765

ELSE

loops = (flen/32765) + 1

END IF

ELSE

loops = 1

END IF

// Read the file

new_pos = 1

FOR i = 1 to loops

bytes_read = FileRead(li_FileNum, b)

tot_b = tot_b + b

NEXT

FileClose(li_FileNum)

 

7、FileSeek()

功  能:将文件指针移动到指定位置。读写文件时相应函数会自动移动文件指针。

语  法:FileSeek ( fileno, position, origin )

参  数:fileno:integer类型,指定文件句柄(由FileOpen()函数得到);

position:long类型,指定相对于origin参数指定位置的新位置偏移量,以字节为单位;

origin:SeekType枚举类型,指定从哪里开始移动文件指针,即指针移动的基准。有效取值为:

FromBeginning! - 缺省值,从文件开头移动指针;

FromCurrent! - 从当前位置移动文件指针;

FromEnd! - 从文件结尾处移动文件指针。

返回值:Long。函数执行成功时返回指针移动后的指针位置。如果任何参数的值为NULL,那么FileSeek()函数返回NULL。

示  例:This example positions the file pointer 14 bytes from the end of the file:

integer li_FileNum

li_FileNum = FileOpen("emp_data")

FileSeek(li_FileNum, -14, FromEnd!)

 

This example moves the file pointer from its current position 14 bytes toward the end of the file. In this case, if no processing has occurred after FileOpen to affect the file pointer, specifying FromCurrent! is the same as specifying FromBeginning!:

integer li_FileNum

li_FileNum = FileOpen("emp_data")

FileSeek(li_FileNum, 14, FromCurrent!)

 

8、FileWrite()

功  能:向指定文件中写数据。

语  法:FileWrite (fileno , variable )

参  数:fileno:integer类型,指定文件句柄(由FileOpen()函数得到);

variable:string或blob类型,其值将写入fileno参数指定的文件。

返回值:Integer。函数执行成功时返回写入文件的字符或字节数,发生错误时返回-1。如果任何参数的值为NULL,那么FileWrite()函数返回NULL。

用  法:FileWrite()函数从当前文件指针开始写入指定数据,写入之后,将文件指针调整到刚刚写入数据的下一个字节位置。当文件以writemode参数设置为Replace!方式打开时,文件指针最初位于文件的开头位置;当文件以writemode参数设置为Append!方式打开时,文件指针最初位于文件的结尾位置。

当文件以行模式打开时,执行FileWrite()函数时,该函数自动在每次写入数据的后面加上回车换行符,并把文件指针移动到回车换行符后面。

当文件以流模式打开时,FileWrite()函数一次最多写入32,765个字节。如果variable参数中数据的长度超过了32,765个字节,那么FileWrite()函数只向文件中写入前32,765个字符并返回32,765。

示  例:This script excerpt opens EMP_DATA.TXT and writes the string New Employees at the end of the file. The variable li_FileNum stores the number of the opened file:

integer li_FileNum

li_FileNum = FileOpen("C:\HR\EMP_DATA.TXT", LineMode!, Write!, LockWrite!, Append!)

FileWrite(li_FileNum, "New Employees")

       

The following example reads a blob from the database and writes it to a file. The SQL SELECT statement assigns the picture data to the blob Emp_Id_Pic. Then FileOpen opens a file for writing in stream mode and FileWrite writes the blob to the file. You could use the Len function to test whether the blob was too big for a single FileWrite call:

integer li_FileNum

blob emp_id_pic

SELECTBLOB salary_hist

        INTO  : emp_id_pic

        FROM Employee

        WHERE Employee.Emp_Num = 100

        USING Emp_tran;

li_FileNum = FileOpen("C:\EMPLOYEE\EMP_PICS.BMP",StreamMode!, Write!, Shared!, Replace!)

FileWrite(li_FileNum, emp_id_pic)

 

9、GetFileOpenName()

功  能:显示打开文件对话框,让用户选择要打开的文件。

语  法:GetFileOpenName(title,pathname,filename{,extension{,filter}})

参  数:title:string类型,指定对话框的标题;

pathname:string类型变量,用于保存该对话框返回的文件路径及文件名;

filename:string类型变量,用于保存该对话框返回的文件名;

extension:string类型,可选项,使用1到3个字符指定缺省的扩展文件名;

filter:string类型,可选项,其值为文件名掩码,指定显示在该对话框的列表框中供用户选择的文件名满足的条件(比如*.*,*.TXT,*.EXE等)。

返回值:Integer。函数执行成功时返回1;当用户单击了对话框上的“Cancel”按钮时函数返回0;发生错误时返回-1。如果任何参数的值为NULL,那么GetFileOpenName()函数返回NULL。

用  法:filter参数的格式为:

description,*. Ext

缺省值为:

"All Files (*.*),*.*"

其中,description说明扩展名的意义,比如“所有文件”、“文本文件”等。你可以根据需要指定在打开文件对话框中显示的文件名类型。当需要指定多种文件类型时,各类型之间使用逗号分隔,例如:

"PIF 文件, *.PIF, 批处理文件, *.BAT"

需要注意的是,该函数只是得到一个文件名,而并没有打开文件,需要打开文件时,依然需要使用FileOpen()函数。

示  例:The following example displays a Select File dialog box that allows multiple selection. The file types are TXT, DOC, and all files, and the initial directory is C:\Program Files\Sybase. The option flag 18 specifies that the Explorer-style dialog box is used (2^1 = 2), and the Read Only check box is hidden (2^4 = 16). The selected filenames are displayed in a MultiLineEdit control.

string docpath, docname[]

integer i, li_cnt, li_rtn, li_filenum

li_rtn = GetFileOpenName("Select File",docpath, docname[], "DOC", &

    + "Text Files (*.TXT),*.TXT," &

    + "Doc Files (*.DOC),*.DOC," &

    + "All Files (*.*), *.*", &

    "C:\Program Files\Sybase", 18)

IF li_rtn < 1 THEN return

li_cnt = Upperbound(docname)

for i=1 to li_cnt

mle_selected.text += string(docpath)+"\"+(string(docname[i]))+"~r~n"

next

 

In the following example, the dialog box has the title Open and displays text files, batch files, and INI files in the Files of Type drop-down list. The initial directory is d:\temp. The option flag 512 specifies that the old-style dialog box is used and the Network button is hidden (2^9 = 512).

// instance variables

// string is_filename, is_fullname

int   li_fileid

if GetFileOpenName ("Open", is_fullname, is_filename, &

    "txt", "Text Files (*.txt),*.txt,INI Files " &

    + "(*.ini), *.ini,Batch Files (*.bat),*.bat", &

    "d:\temp", 512) < 1 then return

li_fileid = FileOpen (is_fullname, StreamMode!)

FileRead (li_fileid, mle_notepad.text)

FileClose (li_fileid)   

 

10、GetFileSaveName()

功  能:显示保存文件对话框,让用户选择要保存到的文件。

语  法:GetFileSaveName(title,pathname,filename{,extension{,filter}})

参  数:title:string类型,指定对话框的标题;

pathname:string类型变量,用于保存该对话框返回的文件路径及文件名;

filename:string类型变量,用于保存该对话框返回的文件名;

extension:string类型,可选项,使用1到3个字符指定缺省的扩展文件名;

filter:string类型,可选项,其值为文件名掩码,指定显示在该对话框的列表框中供用户选择的文件名满足的条件(比如*.*,*.TXT,*.EXE等)。

返回值:Integer。函数执行成功时返回1;当用户单击了对话框上的“Cancel”按钮时函数返回0;发生错误时返回-1。如果任何参数的值为NULL,那么GetFileSaveName()函数返回NULL。

用  法:filter参数的格式为:

description,*. Ext

缺省值为:

"All Files (*.*),*.*"

其中,description说明扩展名的意义,比如“所有文件”、“文本文件”等。你可以根据需要指定在打开文件对话框中显示的文件名类型。当需要指定多种文件类型时,各类型之间使用逗号分隔,例如:

"PIF 文件, *.PIF, 批处理文件, *.BAT"

需要注意的是,该函数只是得到一个文件名,而并没有打开文件,需要打开文件时,依然需要使用FileOpen()函数。

示  例:These statements display the Select File dialog box. The default file extension is .DOC, the filter is all files, and the initial directory is C:\My Documents. The aFlag option 32770 specifies that an Explorer-style dialog box is used with the Read Only check box selected when the dialog box is created. If a file is selected successfully, its path displays in a SingleLineEdit control:

string ls_path, ls_file

int li_rc

ls_path = sle_1.Text

li_rc = GetFileSaveName ( "Select File", &

ls_path, ls_file, "DOC", &

"All Files (*.*),*.*" , "C:\My Documents", 32770)

IF li_rc = 1 Then

sle_1.Text = ls_path + "\" + ls_file

End If

 

11、FileCopy()

功  能:复制文件。也可以指定是否覆盖同名的目标文件。

语  法:FileCopy ( sourcefile , targetfile {, replace})

参  数:sourcefile:string 类型,指定要复制文件的名称(源文件名称);

targetfile:string 类型,指定要复制到哪一个文件(目标文件名称);

replace:Boolean类型,指定当目标文件已经存在时,是否覆盖同名文件。

    True -- 覆盖同名文件;

    False -- 不覆盖同名文件。

返回值:Integer。函数执行成功时返回1;不能打开源文件时返回 –1;不能写入目标文件时返回 –2。

用  法:当源文件名或目标文件名中未指定全路径时,则根据当前目录搜索路径,即文件的路径是用相对于当前目录的相对路径。

示  例:The following example copies a file from the current directory to a different directory and saves the return value in a variable. It does not replace a file of the same name if one already exists in the target directory:

integer li_FileNum

li_FileNum = FileCopy ("jazz.gif" , "C:\emusic\jazz.gif", FALSE)

 

12、FileMove()

功  能:移动文件。

语  法:FileMove ( sourcefile , targetfile )

参  数:sourcefile:string 类型,指定要移动文件的名称(源文件名称);

targetfile:string 类型,指定要移动后文件的名称(目标文件名称);

返回值:Integer。函数执行成功时返回1;不能打开源文件时返回 –1;不能写入目标文件时返回 –2。

用  法:如果目标文件存在,那么函数的执行将失败。

示  例:This example moves a file from the current directory to a different directory and saves the return value in the li_FileNum variable:

integer li_FileNum

li_FileNum = FileMove ("june.csv", "H:/project/june2000.csv" )

 

13、ChangeDirectory()

功  能:改变当前目录。

语  法:ChangeDirectory ( Directoryname )

参  数:Directoryname:string 类型,要设置为当前目录的目录的名称。

返回值:Integer。函数执行成功时返回1;否则返回–1。

示  例:This example changes the current directory to the parent directory of the current directory and displays the new current directory in a SingleLineEdit control:

ChangeDirectory( ".." )

sle_1.text= GetCurrentDirectory( )

 

14、CreateDirectory()

功  能:创建一个目录。

语  法:CreateDirectory ( Directoryname )

参  数:Directoryname:string 类型,要创建的目录的名称。

返回值:Integer。函数执行成功时返回1;否则返回–1。

示  例:This example changes the current directory to the parent directory of the current directory and displays the new current directory in a SingleLineEdit control:

ChangeDirectory( " C:\dgqb " )

sle_1.text= GetCurrentDirectory( )

 

15、DirectoryExists()

功  能:检查指定的目录是否存在。

语  法:DirectoryExists ( Directoryname )

参  数:Directoryname:string 类型,要检查其存在性的目录的名称。

返回值:Boolean。如果指定目录存在,那么返回TRUE;否则返回FALSE。

示  例:his example determines if a directory exists before attempting to move a file to it; otherwise it displays a message box indicating that the path does not exist:

string  ls_path="monthly targets"

If DirectoryExists ( ls_path ) Then

    FileMove ("2000\may.csv", ls_path+"\may.csv" )

    MessageBox ("File Mgr", "File moved to " + ls_path + ".")

Else

    MessageBox ("File Mgr", "Directory " + ls_path + " does not exist" )

End If

 

16、GetCurrentDirectory()

功  能:得到应用程序的当前目录。

语  法:GetCurrentDirectory ( )

返回值:string。返回应用程序当前目录的全路径表示。

示  例:This example puts the current directory name in a SingleLineEdit control:

sle_1.text = GetCurrentDirectory( )

 

17、RemoveDirectory()

功  能:删除指定目录。

语  法:RemoveDirectory ( directoryname )

参  数:directoryname:string类型,要删除的目录的名称。

返回值:Integer。函数执行成功时返回1;否则返回–1。

用  法:要使该函数执行成功,指定的目录必须是空目录,并且不能是当前目录。

示  例:This example removes a subdirectory from the current directory:

string  ls_path="my targets"

integer li_filenum

li_filenum = RemoveDirectory ( ls_path )

If li_filename <> 1 then

MessageBox("Remove directory failed", &

+ "Check that the directory exists, is empty, and " &

+ "is not the current directory")

else

MessageBox("Success", "Directory " + ls_path + " deleted")

end if

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值