注册 登录
编程论坛 VFP论坛

关于Fwrite()的问题

pjtyzyq 发布于 2021-04-28 20:22, 1784 次点击
这是VFP帮助里的例子,加了一行“FWRITE(gnFileHandle, "abcd" )”,希望将字符串“abcd”写入文件,却没有成功,问题出在哪?
Local gnFileHandle,nSize,cString
gnFileHandle = FOPEN("D:\kejian\file_s\sd.txt")
* Seek to end of file to determine number of bytes in the file.
nSize =  FSEEK(gnFileHandle, 0, 2)     && 移动指针到末尾
IF nSize <= 0
 * If file is empty, display an error message.
 WAIT WINDOW "This file is empty!" NOWAIT
ELSE
 * If file is not empty, store the file's contents in memory
 * and display the text in the main Visual FoxPro window.
 = FSEEK(gnFileHandle, 0, 0)      && 移动指针到开始
 cString = FREAD(gnFileHandle, nSize)
  =FWRITE(gnFileHandle, "abcd" )
ENDIF
= FCLOSE(gnFileHandle)         && 关闭文件

[此贴子已经被作者于2021-4-28 20:24编辑过]

3 回复
#2
sdta2021-04-28 20:46
gnFileHandle = FOPEN("D:\kejian\file_s\sd.txt", 12) && 默认为 0 ,只读
#3
sdta2021-04-28 20:51
fopen( ) 函数示例
if file('errors.txt')  && 文件存在吗?
    gnerrfile = fopen('errors.txt',12)  && 如果存在,以读写方式打开
else
    gnerrfile = fcreate('errors.txt')  && 如果不存在,创建文件
endif
if gnerrfile < 0  && 检查文件打开时的错误
    wait 'cannot open or create output file' window nowait
else  && 如果没有错误,则写文件
    =fwrite(gnerrfile, 'error information to be written here')
endif
=fclose(gnerrfile)  && 关闭文件
modify file errors.txt nowait  && 在编辑窗口中打开文件
#4
pjtyzyq2021-04-28 20:55
回复 2楼 sdta
谢谢!
1