文件管理....

首先在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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

睡在床板下_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值