先看例子,该例子功能是将指定的文件夹内的文件移动到新的文件里
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <direct.h>
#include <sys/stat.h>
#include <string>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
static map<string,string> file;
int isdir(const char* s)
{
struct stat mstat;
stat(s,&mstat);
if(mstat.st_mode & S_IFDIR)
{
return 1;
}
else
{
return 0;
}
}
int opend(const char* s)
{
string s1 = s;
string::size_type pos = s1.find_last_of("/");
s1 = s1.substr(0,pos);
_finddata_t mdata;
int filesign = _findfirst(s,&mdata);
if(filesign == -1)
{
return 0;
}
while(strcmp(mdata.name,".")==0 || strcmp(mdata.name,"..")==0)
{
if(-1 == _findnext(filesign,&mdata))
return 0;
}
do
{
string s3 = s1+ "/"+string(mdata.name);
if(1==isdir(s3.c_str()))
{
string s2=s3+"/*";
opend(s2.c_str());
}
else
{
file.insert(pair<string,string>(s3,string("e:/code/")+string(mdata.name)));
}
}while(_findnext(filesign,&mdata) == 0);
}
void copyfile()
{
/* char c;
map<string,string>::iterator it = file.begin();
for(;it!=file.end();++it)
{
FILE* fp = fopen(it->first.c_str(),"r");
FILE* fp1 = fopen(it->second.c_str(),"w");
while(1)
{
c = fgetc(fp);
if(c == EOF)
{
break;
}
else
{
fputc(c,fp1);
}
}
fclose(fp);
fclose(fp1);
}
*/
char* sss = (char*)malloc(500);
map<string,string>::iterator it = file.begin();
for(;it!=file.end();++it)
{
ofstream fout(it->second.c_str(),ios::out);
ifstream fin(it->first.c_str(),ios::in);
while(fin.peek()!= EOF)
{
fin.getline(sss,500);
fout<<sss<<"\n";
}
fout.close();
fin.close();
}
}
int main(int argc,char*argv[])
{
FILE* fp = fopen("e:/abc.txt","r");
char* filename = (char*)malloc(200);
memset(filename,0,200);
fgets(filename,100,fp);
string fullname = string(filename)+string("/*");
fclose(fp);
_mkdir("e:/code");
opend(fullname.c_str());
copyfile();
free(filename);
return 0;
}
函数说明:
mode_t st_mode; //文件的类型和存取的权限
nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
off_t st_size; //文件字节数(文件大小)
unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)
unsigned long st_blocks; //块数
time_t st_atime; //最后一次访问时间
time_t st_mtime; //最后一次修改时间
time_t st_ctime; //最后一次改变时间(指属性)
};