一、删除文件
1.c 调用dos命令: system(char *cmd); 如 system( "del d:\\chang\\xxx.txt "); 删除文件
2.调用api: 删除一个文件: int remove(char *filename ); 返回0 成功 -1失败
3.DeleteFile("d:\\new.txt"); //删除一个文件
BOOL DeleteFile(LPCTSTR filename);
4._unlink( filename ) //失败时返回 -1
二、重命名(也可通过后面介绍的 move,copy实现)
rename(oldname,newname) //成功时返回 0
三、新建目录
1.mkdir("c:\\info\\world"); //失败时返回 -1
2.单级(sa 为安全属性,取 NULL 默认),多级目录需自定义
BOOL CreateDirectory(LPCTSTR dirname, LPSECURITY sa);
3.多级
system("mkdir c:\\a\\b\\c");
四、列出目录下的文件信息
void listAll(char *path){ WIN32_FIND_DATA findData; HANDLE listFile;
strcat(path, "\\*"); //代表列出所有文件 //FindFirstFile("d:\\htdocs\\*.txt",&findData) listFile = FindFirstFile(path, &findData); do{ if(lstrcmp(findData.cFileName,TEXT("."))==0|| lstrcmp(findData.cFileName,TEXT(".."))==0) continue; printf("%s\t\t",findData.cFileName); if(findData.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) printf("hidden\t\t"); if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) printf("DIR\t\t"); printf("\n"); }while(FindNextFile(listFile,&findData));}
五、读取文件修改时间
#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <io.h>DWORD ShowFileTime(PFILETIME lptime);int main(int argc, char *argv[]){ //文件属性结构 WIN32_FILE_ATTRIBUTE_DATA wfad; //获取文件属性 GetFileAttributesEx("d:\\htdocs\\index.html",GetFileExInfoStandard,&wfad); ShowFileTime(&(wfad.ftLastWriteTime)); //ShowFileSize(wfad.nFileSizeHigh,wfad.nFileSizeLow); return 0;}DWORD ShowFileTime(PFILETIME lptime){ FILETIME ftLocal; SYSTEMTIME st; FileTimeToLocalFileTime(lptime,&ftLocal); FileTimeToSystemTime(&ftLocal,&st); printf("%4d/%02d/%02d",st.wYear,st.wMonth,st.wDay); return 0;}
六、读取文件大小
1.
findfirst的第三个参数可以具有下列值:
#define FA_RDONLY 0x01 /* Read only attribute */
#define FA_HIDDEN 0x02 /* Hidden file */
#define FA_SYSTEM 0x04 /* System file */
#define FA_LABEL 0x08 /* Volume label */
#define FA_DIREC 0x10 /* Directory */
#define FA_ARCH 0x20 /* Archive */
#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <io.h>DWORD ShowFileSize(DWORD dwHigh,DWORD dwLow);int main(int argc, char *argv[]){ //文件属性结构 WIN32_FILE_ATTRIBUTE_DATA wfad; //获取文件属性 GetFileAttributesEx("d:\\test.txt",GetFileExInfoStandard,&wfad); ShowFileSize(wfad.nFileSizeHigh,wfad.nFileSizeLow); return 0;}DWORD ShowFileSize(DWORD dwHigh,DWORD dwLow){ ULONGLONG liFileSize; liFileSize = dwHigh; liFileSize <<= sizeof(DWORD)*8; liFileSize += dwLow; printf("%I64u\n",liFileSize); return 0;}
2.直接调用系统函数,更方便。fsh 可设置为 NULL。
DWORD GetFileSize(HANDLE hf, LPDWORD fsh);
七、实现所有文件的遍历
#include <windows.h>#include <stdio.h>#include <stdlib.h>DWORD listAllFile(LPSTR path);DWORD listAllFile(LPSTR path){ WIN32_FIND_DATA findData; HANDLE listFile; char fullPath[100]; char tempPath[100]; lstrcpy(tempPath,path); lstrcat(tempPath,"\\*"); listFile = FindFirstFile(tempPath,&findData); do{ if(lstrcmp(findData.cFileName,TEXT("."))==0|| lstrcmp(findData.cFileName,TEXT(".."))==0) continue; //printf("%s\t\t",findData.cFileName); //构成全路径 wsprintf (fullPath,"%s\\%s",path,findData.cFileName); printf("%s\n",fullPath); /*if(findData.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) printf("hidden\t\t");*/ if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY){ listAllFile(fullPath); } }while(FindNextFile(listFile,&findData)); return 0;}int main(int argc, char *argv[]){ listAllFile("e:\\linuxBooks"); return 0;}
八、文件读操作
#include <stdio.h>#include <stdlib.h>int main(){ int ch; FILE *fp; fp =fopen("aaa.txt","w"); while((ch = getc(fp)) != EOF){ putchar(ch); } fclose(fp); return 0;}
九、删除目录
1.rmdir( "ugg "); // 删除一个目录,空
2.删除存在的空目录
BOOL RemoveDirectory(LPCTSTR path);
3.system("rmdir /s/q ugg"); //删除非空文件夹
十、检查文件存在性及权限
1.
int _access(const char *path, int mode);mode Value Checks File For00 Existence only02 Write permission04 Read permission06 Read and write permission
2.自定义函数
BOOL FileExists(LPCTSTR filename, BOOL dircheck) { DWORD dwa = GetFileAttributes(filename); if (dwa == 0xFFFFFFFF){ /* error */ return FALSE; } /* is a dir */ if ((dwa & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY){ if (dircheck){ return TRUE; }else{ return FALSE; } } else{ /* is a file */ if (!dircheck){ return TRUE; }else{ return FALSE; } } }
十一、文件的复制与移动
//不覆盖 if(CopyFile("d:\\test.txt","c:\\test.txt",TRUE)) puts("文件复制成功"); if(CopyFile("d:\\test.txt","c:\\test.txt",FALSE)) puts("文件覆盖成功"); if(MoveFile("d:\\test.txt","c:\\test.txt")) puts("move");
另一个移动函数的原型(可覆盖)dwflags :MOVEFILE_REPLACE_EXISTING (替换原有文件)
BOOL MoveFileEx(LPCTSTR src, LPCTSTR dest, DWORD dwflags);
来源:https://www.cnblogs.com/xiangzi888/archive/2011/10/22/2220985.html