Input Output functions allow you to read and write to
files. Most of these functions begin with anfprefix. Expand the
category to view a list of the available functions.
Function Name | Description |
fclose | Closes a file. |
feof | Checks 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. |
例子:The
following example opens a log file and writes the string to it using fputc.
//预处理命令之条件编译,如果已经定义了#define,取#ifdef,现取#else部分
#ifdef unix
char *
filename = "/tmp/logfile.txt";
#else
char *
filename = "D:\\logfile.txt"; //运行前现在路径下建立文件
#endif
long file;
//使用long来取代文件指针File
*fp来声明变量
char * p, str[]
= "this is the first line of the log file";
int c;
//创建新文件
//通过判断file的值与NULL是不是相等来验证文件是否正常打开
if
((file=fopen(filename,"w+"))==NULL) {
lr_output_message("Unable
to create %s",filename);
return
-1;
}
//向文件中写入字符串
p = str; //p指针指向数组str的第一个字符
//文本文件结束标记EOF,十进制为-1,十六进制为oxff,while后跟一空语句
while ((*p !=
NULL) && fputc(*(p++), file) != -1);
fclose(file);
例子:The
following example, for Windows platforms, opens a file and reads 3 characters
into a buffer using fgetc.
#define NUM_CHARS
3
int
i,total=0;
char
buffer[100],ch;
long file;
char
*filename="D:\\readme.txt";
//只读方式打开文件,并且file指向文件,如果为NULL,文件为正常打开
if((file=fopen(filename,"r"))==NULL) {
lr_error_message ("Cannot open %s", filename);
return
-1;
}
//feof未读取到文件末尾返回0,否则返回非零值
for (i=0;
(i<NUM_CHARS) && (feof(file) == 0); i++) {
ch=fgetc(file);
buffer[i]=ch;
}
buffer[i]=NULL;
lr_output_message("First %d characters of file %s are
\"%s\"",NUM_CHARS,filename,buffer);
if
(fclose(file))//文件关闭成功返回0,否则返回EOF
lr_error_message ("Error closing file %s", filename);
例子:The
following example, for Windows platforms, opens a file and reads the first line
using fgets.
int
i,total=0;
char
line[100],ch;
long file;
char
*filename ="D:\\readme.txt";
////通过判断 file的值与NULL是不是相等来验证文件是否正常打开
if((file=fopen(filename,"r"))==NULL)
{
lr_error_message("Cannot
open %s",filename);
return
-1;
}
//获得文件中第一行字符串
if((fgets(line,100,file)==NULL))
lr_output_message("fgets error");
else
lr_output_message("The
first line is \"%s\"",line);
if(fclose(file))
lr_error_message("Error
closing file %s",filename);
例子:The
following example opens a log file and writes the id number and group name of
the Virtual User to it using fprintf.
//预处理命令之条件编译,如果已经定义了#define,取#ifdef,现取#else部分
#ifdef unix
char *
filename = "/tmp/logfile.txt";
#else
char *
filename = "D:\\logfile.txt";
#endif
long file;
int id;
char *
groupname;
//<span style='fon