首先在c 目录下 建立一个ying的目录,又在此目录下建一个cheng.txt的文件
1:检查文件是否存在
#include<io.h>
#include<sys/stat.h>
#include<iostream.h>
int main()
{
int retval;
char *str="c://cheng.txt";
retval=_access(str,0);
if(retval==1)
cout<<"not find"<<endl;
else
cout<<"find"<<endl;
return 0;
}
2:检查文件是否是一个目录
#include<iostream.h>
#include <windows.h>
int main()
{
int result;
char *str="c://ying";
result=GetFileAttributes(str);
if(result==0xffffffff)
cout<<"not find"<<endl;
else
{
if(result&FILE_ATTRIBUTE_DIRECTORY)
cout<<"是目录"<<endl;
else{
cout<<"不是目录"<<endl;
}
}
return 0;
}
3:检查文件是否可读,是否可写
int result1;
int result2;
char *str="c://ying//cheng.txt";
result1=_access(str,4);//read
result2=_access(str,2);//write
4:返回文件以字节计算的长度
#include<io.h>
#include<iostream.h>
#include<fcntl.h>
//#include <windows.h>
int main()
{
int result;
char *str="c://ying//cheng.txt";
result=_open(str,_O_RDONLY);
if(result==-1)
cout<<"not find"<<endl;
else
{
long size;
size=_filelength(result);
if(size==-1L)
{
cout<<"is null"<<endl;
}
else
cout<<size<<endl;
_close(result);
}
return 0;
}
5:返回文件最后一次修改的时间,返回为秒数,从1970年1月1日的子夜(00:00:00)开始
#include<io.h>
#include<sys/stat.h>
#include<direct.h>
#include<iostream.h>
#include<fcntl.h>
int main()
{
int result;
char *str="c://ying//cheng.txt";
result=_open(str,_O_RDONLY);
if(result==-1)
cout<<"not find"<<endl;
else
{
struct _stat fields;
int handle;
handle=_fstat(result,&fields);
if(handle==-1)
{
cout<<"is null"<<endl;
}
else
cout<<fields.st_mtime<<endl;
_close(result);
}
return 0;
}
6删除一个文件
#include<io.h>
#include<sys/stat.h>
#include<iostream.h>
int main()
{
//int retval;
char *str="c://cheng.txt";
if(remove(str)==-1)
cout<<"not remove"<<endl;
else
cout<<"remove"<<endl;
return 0;
}
7:创建一个目录
#include<io.h>
#include<sys/stat.h>
#include<direct.h>
#include<iostream.h>
int main()
{
//int retval;
char *str="c://cheng";
if(_mkdir(str)==-1)
cout<<"not create"<<endl;
else
cout<<"create"<<endl;
return 0;
}
8:删除一个目录
#include<io.h>
#include<sys/stat.h>
#include<direct.h>
#include<iostream.h>
int main()
{
//int retval;
char *str="c://cheng";
if(_rmdir(str)==-1)
cout<<"not delete"<<endl;
else
cout<<"delete"<<endl;
return 0;
}
9:返回一个目录的绝对路径
_fullpath(char *di,char *sour,int size)
10:返回一个目录的内容
#include<iostream.h>
//#include<fcntl.h>
#include<string.h>
#include<stdio.h>
char buffer[16*20];
int main()
{
//int retval;
char *str="c://ying";
FILE *pipe;
strcpy(buffer,"dir ");
strcat(buffer,str);
strcat(buffer," /b");
pipe=_popen(buffer,"rb");
if(pipe==NULL)
cout<<"not find"<<endl;
else {
fread(buffer,1,16*20,pipe);
}
_pclose(pipe);
cout<<buffer<<endl;
return 0;
}