您的位置:首页 > 编程语言

lr中读写文件操作代码(原创)

2005-04-13 16:35 435 查看
1.lr中支持的读写函数有:

Function Name

Description

fclose

Closes a file.
feof

Check

s if the end of file has occurred on a stream.

ferror

Checks if any error has occurred during file I/0.
fgetc

Gets a character from a stream.
fgets

Reads a string from a file.
fopen

Opens a file for buffered I/0.
fprintf

Writes formatted output to a file.
fputc

Writes a character to a stream.
fread

Reads unformatted data from a stream into a buffer.
fscanf

Reads formatted input from a stream.
fseek

Sets the current position in a file to a new location.
fwrite

Write unformatted data from a buffer to a stream.
rewind

Rewinds a file.
sprintf

Writes formatted output to a string.
sscanf

Reads formatted input from a string.
2. 以下的例子是打开文件并追加写入该文件

char *pr;

char *filename= "e://log.txt";

long file;

char *p;

if ((file = fopen(filename, "at")) == NULL ) {

          lr_error_message("Cannot open %s", filename);

          return -1;

     }

 p = lr_eval_string("{pr}");

 

 while ((*p != NULL) && fputc(*(p++), file) != -1); 

 

 fputc("\n",file);

 fclose(file);

3.补充c函数知识:

文件使用方式        意 义

“rt”      只读打开一个文本文件,只允许读数据

“wt”      只写打开或建立一个文本文件,只允许写数据

“at”      追加打开一个文本文件,并在文件末尾写数据

“rb”      只读打开一个二进制文件,只允许读数据

“wb”       只写打开或建立一个二进制文件,只允许写数据

“ab”       追加打开一个二进制文件,并在文件末尾写数据

“rt+”      读写打开一个文本文件,允许读和写

“wt+”      读写打开或建立一个文本文件,允许读写

“at+”      读写打开一个文本文件,允许读,或在文件末追加数 据

“rb+”      读写打开一个二进制文件,允许读和写

“wb+”      读写打开或建立一个二进制文件,允许读和写

“ab+”      读写打开一个二进制文件,允许读,或在文件末追加数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐