📄 io.h
字号:
@函数名称: open
函数原型: int open(char * filename,int mode);
函数功能: 以mode指出的方式打开已存在的名为filename的文件.
函数返回: 返回文件号.如果打开失败,返回-1.
参数说明: 非ANSI标准函数. filename 要打开的文件路径和名称
mode 访问模式,宏定义和含义如下:
O_RDONLY 1 只读打开
O_WRONLY 2 只写打开
O_RDWR 4 读写打开
还可选择以下模式与以上3种基本模式相与:
O_CREAT 0x0100 创建一个文件并打开
O_TRUNC 0x0200 打开一个已存在的文件并将文件长度设置为0,其他属性保持
O_EXCL 0x0400 未使用
O_APPEND 0x0800 追加打开文件
O_TEXT 0x4000 打开文本文件翻译CR-LF控制字符
O_BINARY 0x8000 打开二进制字符,不作CR-LF翻译
mode 该参数仅在access=O_CREAT方式下使用,其取值如下:
S_IFMT 0xF000 文件类型掩码
S_IFDIR 0x4000 目录
S_IFIFO 0x1000 FIFO 专用
S_IFCHR 0x2000 字符专用
S_IFBLK 0x3000 块专用
S_IFREG 0x8000 只为0x0000
S_IREAD 0x0100 可读
S_IWRITE 0x0080 可写
S_IEXEC 0x0040 可执行
所属文件: <io.h>,<fcntl.h>,<sys\stat.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
int handle;
char msg[]="Hello world";
if ((handle=open("TEST.dat",O_CREAT | O_TEXT))==-1)
{
perror("Error:");
return 1;
}
write(handle,msg,strlen(msg));
close(handle);
return 0;
}
@函数名称: close
函数原型: int close(int fp);
函数功能: 关闭文件.
函数返回: 成功返回0,不成功返回 -1.
参数说明: 非ANSI标准.fp 要关闭的文件句柄(由open()函数产生).
所属文件: <io.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
int handle;
char buf[11]="0123456789";
handle=open("NEW.txt",O_CREAT);
if (handle>-1)
{
write(handle,buf,strlen(buf));
close(handle);
}
else
printf("Error opening file");
return 0;
}
@函数名称: creat
函数原型: int creat(char * filename,int mode);
函数功能: 以mode所指定的方式建立文件.
函数返回: 成功返回正数,不成功返回-1.
参数说明: 非ANSI标准
所属文件: <io.h>
#include <sys\stat.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>
int main()
{
int handle;
char buf[11]="0123456789";
_fmode=O_BINARY;
handle=creat("new.txt",S_IREAD | S_IWRITE);
write(handle,buf,strlen(buf));
close(handle);
return 0;
}
@函数名称: eof
函数原型: int eof(int fd);
函数功能: 检查文件是否结束.
函数返回: 遇文件结束返回1,否则返回0,-1操作出错.
参数说明: 非ANSI标准;fd-已打开的文件句柄
所属文件: <io.h>
#include <io.h>
int main()
{
int handle;
char msg[]="Ping yesky...";
char ch;
handle=open("test.txt",O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
write(handle,msg,strlen(msg));
lseek(handle,0L,SEEK_SET);
do{
read(handle,&ch,1);
printf("%c",ch);
}while (!eof(handle));
close(handle);
return 0;
}
@函数名称: read
函数原型: int read(int fd,char * buf,unsigned count);
函数功能: 从文件号fd所指示的文件中读count个字节到由buf指示的缓冲区中.
函数返回: 返回真正读入的字节个数.如遇文件结束返回0,出错返回-1.
参数说明: 非ANSI标准函数.buf 读出的数据.
所属文件: <io.h>
#include <stdio.h>
#include <io.h>
#include <alloc.h>
#include <fcntl.h>
#include <process.h>
#include <sys\stat.h>
int main()
{
void *buf;
int handle,bytes;
buf=malloc(10);
if ((handle=open("TEST.$$$",O_RDONLY | O_BINARY,S_IWRITE | S_IREAD))==-1)
{
printf("Error Opening File");
exit(1);
}
if ((bytes=read(handle,buf,10))==-1)
{
printf("Read Failed.");
exit(1);
}
else
printf("Read: %d bytes read.",bytes);
return 0;
}
@函数名称: write
函数原型: int write(int fd,char * buf,unsigned count);
函数功能: 从buf指示的缓冲区输出count个字符到fd所标志的文件中.
函数返回: 返回实际输出的字节数,如果出错返回-1.
参数说明: 非ANSI标准函数.fd-文件句柄,buf-写入内容,count-写入内容的长度
所属文件: <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#include <string.h>
int main()
{
int handle;
char string[40];
int length,res;
handle=open("TEST.$$$",O_WRONLY | O_CREAT | O_TRUNC,S_IREAD | S_IWRITE)
if (handle==-1)
{
printf("Error opening file.");
exit(1);
}
strcpy(string,"Hello,world!");
length=strlen(string);
if ((res=write(handle,string,length))!=length)
{
printf("Error writing to the file.");
exit(1);
}
printf("Wrote %d bytes to the file.",res);
close(handle);
return 0;
}
@函数名称: filelength
函数原型: long filelength(int handle)
函数功能: 得到文件长度
函数返回: 返回文件长度,操作失败时返回 -1
参数说明: handle-用open()函数打开的文件句柄
所属文件: <io.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
int handle;
char buf[11]="123456789A";
handle=open("DUMMY.FIL",O_CREAT);
write(handle,buf,strlen(buf));
printf("file length in bytes: %ld",filelength(handle));
close(handle);
return 0;
}
@函数名称: tell
函数原型: long tell(int handle)
函数功能: 得到文件handle的位置指示器数值
函数返回: 文件指示器位置
参数说明: handle-使用open打开的文件句柄
所属文件: <io.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
int handle;
char msg[]="Hello world";
if ((handle=open("TEST.$$$",O_CREAT | O_TEXT | O_APPEND))==-1)
{
perror("Error:");
return 1;
}
write(handle,msg,strlen(msg));
printf("The file pointer is at byte %ld",tell(handle));
close(handle);
return 0;
}
@函数名称: lseek
函数原型: long lseek(int handle,long offset,int fromwhere)
函数功能: 移动文件位置指针到指定位置
函数返回:
参数说明: handle-文件句柄,offset-文件位置,fromwhere-从文件何处开始移动,该参数可使用以下宏定义:
SEEK_SET-0从文件开始位置计算offset
SEEK_CUR-1从当前位置计算offset
SEEK_END-2从文件结束位置计算offset,此时offset为负数
所属文件: <io.h>
#include <sys\stat.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
int handle;
char msg[]="This is a test";
char ch;
handle=open("TEST.$$$",O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
write(handle,msg,strlen(msg));
lseek(handle,0L,SEEK_SET);
do{
read(handle,&ch,1);
printf("%c",ch);
}while (!eof(handle));
close(handle);
return 0;
}
@函数名称: getftime
函数原型: int getftime(int handle,struct ftime *ftptr)
函数功能: 得到由handle指向的文件的生成日期和时间
函数返回: 0:操作成功,-1:操作失败
参数说明: fptr-磁盘驱动器的信息.handle-文件句柄
所属文件: <dos.h>
#include <stdio.h>
#include <io.h>
int main()
{
FILE *stream;
struct ftime ft;
if ((stream=fopen("TEST.$$$","wt")) == NULL)
{
fprintf(stderr,"Cannot open output file.");
return 1;
}
getftime(fileno(stream),&ft);
printf("File time: %u:%u:%u",ft.ft_hour,ft.ft_min,ft.ft_tsec*2);
printf("File date: %u/%u/%u",ft.ft_month,ft.ft_day,ft.ft_year+1980);
fclose(stream);
return 0;
}
@函数名称: chmod
函数原型: int chmod(char *filename,int mode)
函数功能: 改变文件访问模式
函数返回:
参数说明:
所属文件: <io.h>
#include <sys\stat.h>
#include <stdio.h>
#include <io.h>
void make_read_only(char *filename);
int main()
{
make_read_only("NOTEXIST.FIL");
make_read_only("MYFILE.FIL");
return 0;
}
void make_read_only(char *filename)
{
int stat;
stat=chmod(filename,S_IREAD);
if (stat)
printf("Couldn't make %s read-only",filename);
else
printf("Made %s read-only",filename);
}
@函数名称: setmode
函数原型: int setmode(int handle,int amode)
函数功能: 重新设置一个已打开文件的操作模式
函数返回: 0:操作成功,-1:操作失败
参数说明: handle-已打开文件句柄,amode-打开模式,同open函数中的参数mode
所属文件: <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
int result;
result=setmode(fileno(stdprn),O_TEXT);
if (result == -1)
perror("Mode not available");
else
printf("Mode successfully switched");
return 0;
}
@函数名称: dup
函数原型: int dup(int handle)
函数功能: 得到文件handle的新说明符
函数返回: 新的文件句柄
参数说明: handle 已经打开的文件句柄
所属文件: <io.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
void flush(FILE *stream);
int main()
{
FILE *fp;
char msg[]="This is a test";
fp=fopen("DUMMY.FIL","w");
fwrite(msg,strlen(msg),1,fp);
clrscr();
printf("Press any key to flush DUMMY.FIL:");
getch();
flush(fp);
printf("File was flushed,Press any key to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
fflush(stream);
duphandle=dup(fileno(stream));
close(duphandle);
}
@函数名称: isatty
函数原型: int isatty(int handle)
函数功能: 查询文件句柄是否与控制台、终端、打印机、串口相连接
函数返回: 非0-相连接,0-不相连接
参数说明: handle-要查询的文件句柄
所属文件: <io.h>
#include <stdio.h>
#include <io.h>
int main()
{
int handle;
handle=fileno(stdprn);
if (isatty(handle))
printf("Handle %d is a device type",handle);
else
printf("Handle %d isn't a device type",handle);
return 0;
}
@函数名称: lock
函数原型: int lock(int handle,long offset,long length)
函数功能: 对文件中的某个区域进行锁定操作
函数返回: 0-锁定成功,非0-锁定失败
参数说明: 锁定后,禁止其他程序操作该区域,用于支持网络环境下的文件共享操作
handle-文件句柄
offset-文件区域的起始位置
length-文件区域的长度
所属文件: <io.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <process.h>
#include <share.h>
#include <stdio.h>
int main()
{
int handle,status;
long length;
handle=sopen("c:\msdos.sys",O_RDONLY,SH_DENYNO,S_IREAD);
if (handle<0)
{
printf("sopen failed");
exit(1);
}
length=filelength(handle);
status=lock(handle,0L,length/2);
if (status==0)
printf("lock succeeded");
else
printf("lock failed");
status=unlock(handle,0L,length/2);
if (status==0)
printf("unlock succeeded");
else
printf("unlock failed");
close(handle);
return 0;
}
@函数名称: fdopen
函数原型: FILE *fdopen(int handle,char *mode)
函数功能: 得到一个与UNIX文件句柄handle等价的流式文件指针
函数返回: 流式文件指针
参数说明: mode 同使用handle打开时的mode参数
所属文件: <stdio.h>
#include <sys\stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
int handle;
FILE *stream;
handle=open("DUMMY.FIL",O_CREAT,S_IREAD | S_IWRITE);
stream=fdopen(handle,"w");
if (stream == NULL)
printf("fdopen failed");
else
{
fprintf(stream,"Hello world");
fclose(stream);
}
return 0;
}
@函数名称: access
函数原型: int access(char * filename,int mode)
函数功能: 文件状态检查,UNIX函数,非ANSIC C标准函数
函数返回: 0:允许访问,-1:禁止访问
参数说明: finename-文件名称
mode-模式,共5种模式;
0-检查文件是否存在
1-检查文件是否可运行
2-检查文件是否可写访问
4-检查文件是否可读访问
6-检查文件是否可读/写访问
所属文件: <io.h>
#include <stdio.h>
#include <io.h>
int file_exists(char *filename);
int main()
{
printf("Does myfile.FIL exist: %s",file_exists("myfile.FIL") ? "YES" : "NO");
return 0;
}
int file_exists(char *filename)
{
return (access(filename,0)==0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -