文件操作相关的函数和类
获取文件的时间。
bool FileMTime(const char *filename,char *mtime,const char *fmt=0);
获取文件的大小。
int FileSize(const char *filename);
int FileSize(const char *filename)
{
struct stat st_filestat;
if (stat(filename,&st_filestat) < 0) return -1;
return st_filestat.st_size;
}
重置文件的修改时间属性。
bool UTime(const char *filename,const char *mtime);
bool UTime(const char *filename,const char *mtime)
{
struct utimbuf stutimbuf;
stutimbuf.actime=stutimbuf.modtime=strtotime(mtime);
if (utime(filename,&stutimbuf)!=0) return false;
return true;
}
打开文件。
FILE *FOPEN(const char *filename,const char *mode);
FILE *FOPEN(const char *filename,const char *mode)
{
if (MKDIR(filename) == false) return 0;
return fopen(filename,mode);
}
---测试
int main()
{
FILE *fp=0;
if ( (fp=FOPEN("/tmp/aaa/bbb/ccc/tmp.xml","w"))==0)
{
printf("FOPEN(/tmp/aaa/bbb/ccc/tmp.xml) %d:%s\n",errno,strerror(errno));
return -1;
}
fprintf(fp,"<data>\n"\
"<name>妲已</name><age>28</age><sc>火辣</sc><yz>漂亮</yz><memo>商要亡,关我什么事。</memo><endl/>\n"\
"<name>西施</name><age>25</age><sc>火辣</sc><yz>漂亮</yz><memo>1、中国排名第一的美女;\n"\
"2、男朋友是范蠡;\n"\
"3、老公是夫差,被勾践弄死了。</memo><endl/>\n"\
"</data>\n");
fclose(fp);
}
从文本文件中读取一行。
bool FGETS(const FILE *fp,char *buffer,const int readsize,const char *endbz=0);
bool FGETS(const FILE *fp,char *buffer,const int readsize,const char *endbz)
{
if ( fp == 0 ) return false;
memset(buffer,0,readsize+1);
char strline[readsize+1];
while (true)
{
memset(strline,0,sizeof(strline));
if (fgets(strline,readsize,(FILE *)fp) == 0) break;
if ( (strlen(buffer)+strlen(strline)) >= (unsigned int)readsize ) break;
strcat(buffer,strline);
if (endbz == 0) return true;
else if (strstr(strline,endbz)!= 0) return true;
}
return false;
}
----测试
int main()
{
FILE *fp=0;
if ( (fp=FOPEN("/tmp/aaa/bbb/ccc/tmp.xml","r"))==0)
{
printf("FOPEN(/tmp/aaa/bbb/ccc/tmp.xml) %d:%s\n",errno,strerror(errno));
return -1;
}
char strBuffer[301];
while (true)
{
memset(strBuffer,0,sizeof(strBuffer));
if (FGETS(fp,strBuffer,300,"<endl/>")==false) break;
printf("strBuffer=%s",strBuffer);
}
fclose(fp);
}
删除文件,类似Linux系统的rm命令
bool REMOVE(const char *filename,const int times=1);
bool REMOVE(const char *filename,const int times)
{
if (access(filename,R_OK) != 0) return false;
for (int ii=0;ii<times;ii++)
{
if (remove(filename) == 0) return true;
usleep(100000);
}
return false;
}
把文件重命名,类似Linux系统的mv命令
bool RENAME(const char *srcfilename,const char *dstfilename,const int times=1);
bool RENAME(const char *srcfilename,const char *dstfilename,const int times)
{
if (access(srcfilename,R_OK) != 0) return false;
if (MKDIR(dstfilename) == false) return false;
for (int ii=0;ii<times;ii++)
{
if (rename(srcfilename,dstfilename) == 0) return true;
usleep(100000);
}
return false;
}
复制文件,类似Linux系统的cp命令
// srcfilename:原文件名,建议采用绝对路径的文件名。
// dstfilename:目标文件名,建议采用绝对路径的文件名。
// 返回值:true-成功;false-失败,失败的主要原因是权限不足或磁盘空间不够。
// 注意:
// 1)在复制文件之前,会自动创建dstfilename参数中的目录名。
// 2)复制文件的过程中,采用临时文件命名的方法,复制完成后再改名为dstfilename,避免中间状态的文件被读取。
// 3)复制后的文件的时间与原文件相同,这一点与Linux系统cp命令不同。
bool COPY(const char *srcfilename,const char *dstfilename)
bool COPY(const char *srcfilename,const char *dstfilename)
{
if (MKDIR(dstfilename) == false) return false
char strdstfilenametmp[301]
SNPRINTF(strdstfilenametmp,sizeof(strdstfilenametmp),300,"%s.tmp",dstfilename)
int srcfd,dstfd
srcfd=dstfd=-1
int iFileSize=FileSize(srcfilename)
int bytes=0
int total_bytes=0
int onread=0
char buffer[5000]
if ( (srcfd=open(srcfilename,O_RDONLY)) < 0 ) return false
if ( (dstfd=open(strdstfilenametmp,O_WRONLY|O_CREAT|O_TRUNC,S_IWUSR|S_IRUSR|S_IXUSR)) < 0) { close(srcfd)
while (true)
{
memset(buffer,0,sizeof(buffer))
if ((iFileSize-total_bytes) > 5000) onread=5000
else onread=iFileSize-total_bytes
bytes=read(srcfd,buffer,onread)
if (bytes > 0) write(dstfd,buffer,bytes)
total_bytes = total_bytes + bytes
if (total_bytes == iFileSize) break
}
close(srcfd)
close(dstfd)
// 更改文件的修改时间属性
char strmtime[21]
memset(strmtime,0,sizeof(strmtime))
FileMTime(srcfilename,strmtime)
UTime(strdstfilenametmp,strmtime)
if (RENAME(strdstfilenametmp,dstfilename) == false) { REMOVE(strdstfilenametmp)
return true
}
---测试
int main()
{
if (REMOVE("/project/public/lib_public.a")==false)
{
printf("REMOVE(/project/public/lib_public.a) %d:%s\n",errno,strerror(errno));
}
if (RENAME("/project/public/lib_public.so","/tmp/aaa/bbb/ccc/lib_public.so")==false)
{
printf("RENAME(/project/public/lib_public.so) %d:%s\n",errno,strerror(errno));
}
if (COPY("/project/public/libftp.a","/tmp/root/aaa/bbb/ccc/libftp.a")==false)
{
printf("COPY(/project/public/libftp.a) %d:%s\n",errno,strerror(errno));
}
printf("size=%d\n",FileSize("/project/public/_public.h"));
UTime("/project/public/_public.h","2020-01-05 13:37:29");
char mtime[21]; memset(mtime,0,sizeof(mtime));
FileMTime("/project/public/_public.h",mtime,"yyyy-mm-dd hh24:mi:ss");
printf("mtime=%s\n",mtime);
}