c++文件操作相关

file operation

    • HANDLE CreateFile(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreateDisposition,dwFlagsAndAttributes,hTemplateFile);
      e.g. CreateFile("c:\abc\word.txt",GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL,0/*the file can not be shared*/,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
      * parameter dwCreationDisposition must one of these five values: 2=CREATE_ALWAYS 不论文件是否存在总是创建文件或设备,如果已存在,则原文件被清空
      1=CREATE_NEW 只有当文件不存在的时候才创建一个新文件,如果指定文件已经存在,则创建失败,这是它和OPEN_EXISTING不一样的地方
      4=OPEN_ALWAYS 打开一个文件或设备,如果不存在,则创建一个文件或设备并打开
      3=OPEN_EXISTING 打开一个文件或设备,如果不存在,则打开失败
      5=TRUNCATE_EXISTING打开一个文件或设备并清空,如果不存在,则打开失败
      if this function succeeds, the return value will be a open handle to the specified device, file or maillsot.if the function fails, the return value will be INVALID_HANDLE_VALUE.
    • MoveFile
    • CopyFile(lpExistingFileName,lpNewFileName,bFailIfExists); bFailIfExists,如果为TRUE,则NewFileName存在时COPY失败,如果设置为FALSE,则会用新文件将原有文件覆盖。
    • DeleteFile(lpFileName);如果文件不存在则删除失败,如果文件是只读属性,则也是删除失败,除非之前先去除文件的只读属性,if fails return 0;if succeed return other value;
      typedef struct _SHFILEOPSTRUCT {
      HWND         hwnd;
      UINT         wFunc;
      PCZZTSTR     pFrom;
      PCZZTSTR     pTo;
      FILEOP_FLAGS fFlags;
      BOOL         fAnyOperationsAborted;
      LPVOID       hNameMappings;
      PCTSTR       lpszProgressTitle;
      } SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT;
    • SHFileOperation(LPSHFILEOPSTRUCT lpFileOp)Copies, moves, renames, or deletes a file system object.
    • fstream("xxx.xxx",ios::in|ios::out|ios::trunc)
      related functions:seekg tellg seekp tellp fstream fs; fs.read(buffer,size);
       
      char *readfile(fstream fs,string fname)
      {
      fs.open(fname.c_str());
      if(fs.fail()){cout<<"open failed"<<endl;return 0;
      fs.seekg(0,ios::end);
      int length=tellg();
      char *buffer=new char[length+1];
      fs.seekg(0,ios::beg);
      fs.read(buffer,length);
      fs.close();
      return buffer;
      }
      
    • int rename( const char *oldname, const char *newname );
    • _findfirst
    • _findnext
    • system("move oldname newname")带空格的路径名需要加双引号,移动文件夹和文件
    • system("del /s /q c:\abc\");删除文件夹下的所有文件,子目录下的文件也会被删除,但目录会保留

directory operation

    • MakeSureDirectoryPathExists("c:\aa\bb\cc\"); requirement:include &ltDbghelp.h&gt #pragma(lib,"dbghelp.lib");
    • CreateDirectoryEx(lpTemplateDirectory,lpNewDirectory,lpSecurityAttributes);//不能创建多层目录
    • RemoveDirectory(lpPathName);如果包含子目录,则删除失败
    • _mkdir("\aa"); if the intermidite directory does not exist, the create operation failed.
    • _rmdir("\aa\bb\cc");//只有文件夹是空的时候才有效
    • int rename( const char *oldname, const char *newname );
    • system ("rmdir /s /q c:\aaa\");
    • system("move oldname newname")带空格的路径名需要加双引号,移动文件夹和文件
相信世界是平的
谨记四个字“修身养性”
大江东去浪淘尽英雄,再牛B的人物最后也是一掊土
向善不是目的,而是抚慰心灵,更多的感受幸福,感谢别人给你行善的机会
相信老子的话:万物生于有,有生于无,一切的道理都源于一个无法证明的假设
我是好是坏就自然而然的摆在那里,并不会因为别人的评价而改变什么,我也不需要别人用一张纸来说明我什么,世间最难得的是自由



支持大额赞助:
原文地址:https://www.cnblogs.com/sky-view/p/3902574.html