c/c++文件系统操作[Dev-c++ 4.9.9.2]

/*
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}
*/
#include 
<stdio.h>
#include 
<stdlib.h>
#include 
<io.h>
//const char *to_search="C:\\WINDOWS\\*.txt";//欲查找的文件,支持通配符
const char *to_search="temp\\*.*";//查找当前文件夹下temp文件夹下的所有文件或文件夹
/*
*
struct _finddata_t
{
unsigned attrib;
time_t time_create;//long
time_t time_access;//long
time_t time_write;//long
_fsize_t size;//这里的_fsize_t应该可以相当于unsigned整型,表示文件的字节数
char name[_MAX_FNAME];
};
*/
int main()
{
    
long handle;//用于查找的句柄
    struct _finddata_t fileinfo;//文件信息的结构体
    handle=_findfirst(to_search,&fileinfo);//第一次查找
    if(-1==handle)return -1;
    
//printf("%s\n",fileinfo.name);//打印出找到的文件的文件名
    printf(",%d\n",fileinfo.time_write);//打印出找到的文件的文件名
    while(!_findnext(handle,&fileinfo))//循环查找其他符合的文件,直到找不到其他的为止
    {
       
if(fileinfo.attrib == _A_ARCH){
          printf(
"存档, ");
       }
else if(fileinfo.attrib == _A_NORMAL){
          printf(
"正常, ");
       }
else if(fileinfo.attrib == (_A_HIDDEN | _A_ARCH)){
          printf(
"隐藏, ");
       }
else if(fileinfo.attrib == _A_RDONLY){
          printf(
"只读, ");
       }
else if(fileinfo.attrib == _A_SUBDIR){
          printf(
"文件夹, ");
       }
else if(fileinfo.attrib == _A_SYSTEM){
          printf(
"系统, ");
       }
       printf(
"%s, %d, %d\n",fileinfo.name,fileinfo.attrib,_A_HIDDEN | _A_ARCH);
       
    }
    _findclose(handle);
//别忘了关闭句柄
    
//
    
// 删除指定的文件,只能删除空的文件夹 
    char *removePath = "temp\\test";
    
// 删除文件夹 
    int status = rmdir(removePath);
    
// 删除文件
    
//int status = remove(removePath);
    if(status == -1){
      printf(
"删除失败..\n");
    }
else{
      
// 0表示删除成功 
      printf("删除成功..\n");
    }
    
char *initPath = "temp\\c1.txt";
    
char *newPath = "temp\\c1_copy.txt";
    status 
= rename(initPath, newPath);
    
if(status == -1){
       printf(
"改动失败..\n");
    }
else{
       printf(
"改动成功..\n");
    }
    
// 创建文件夹
    char *newDir = "temp\\testA";
    status 
= mkdir(newDir);
    
if(status == -1){
       printf(
"创建失败..\n");
    }
else{
       printf(
"创建成功..\n");
    }
    
    printf(
"\n%d\n",status);
    
//
    system("pause");
    
return 0;
}

 

posted @ 2010-11-12 11:47  vily_雷  阅读(473)  评论(0编辑  收藏  举报