file operation

  • API functions
    • 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.
  • C/C++ library functions[code]
     
    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;
    }
    • 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
  • Command Line
    • system("move oldname newname")带空格的路径名需要加双引号,移动文件夹和文件
    • system("del /s /q c:\\abc\\");删除文件夹下的所有文件,子目录下的文件也会被删除,但目录会保留

directory operation

  • API functions
    • MakeSureDirectoryPathExists("c:\\aa\\bb\\cc\\"); requirement:include &ltDbghelp.h&gt #pragma(lib,"dbghelp.lib");
    • CreateDirectoryEx(lpTemplateDirectory,lpNewDirectory,lpSecurityAttributes);//不能创建多层目录
    • RemoveDirectory(lpPathName);如果包含子目录,则删除失败
  • C/C++ library functions
    • _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 );
  • Command Line
    • system ("rmdir /s /q c:\\aaa\\");
    • system("move oldname newname")带空格的路径名需要加双引号,移动文件夹和文件

c++文件操作相关的更多相关文章

  1. iOS:文件操作相关(18-03-23更)

    0.iOS文件系统 1.工程内文件 2.文件夹管理 3.文件操作 4.NSCache 附录: 1.沙盒文件夹.文件大小 2.清除沙盒 Library / Cache 下所有数据 3.测试plist 0 ...

  2. Linux(三) - 文件操作相关命令

    Ctl-A 光标移动到行首 Ctl-C 终止命令 Ctl-D 注销登录 Ctl-E 光标移动到行尾 Ctl-U 删除光标到行首的所有字符,在某些设置下,删除全行 Ctl-W 删除当前光标到前边的最近一 ...

  3. Android入门之文件系统操作(二)文件操作相关指令

    (一)获取总根 File[] fileList=File.listRoots(); //返回fileList.length为1 //fileList.getAbsolutePath()为"/ ...

  4. Linux文件操作相关命令

    1.创建文件夹: [root@izuf6ih01h8fzeziddwkfdz sm]# mkdir a 创建一个名为a的文件夹 2.创建文件: [root@izuf6ih01h8fzeziddwkfd ...

  5. Linux 文件操作相关常用命令

    1.创建文件夹 [root@izwz9148jq0xmzrb36r113z 20190726]# mkdir aaa //创建aaa文件夹 2.创建文件 [root@izwz9148jq0xmzrb3 ...

  6. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  7. Node基础篇(文件操作)

    文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...

  8. 第32课 Qt中的文件操作

    1. Qt的中IO操作 (1)Qt中IO操作的处理方式 ①Qt通过统一的接口简化了文件和外部设备的操作方式 ②Qt中的文件被看作一种特殊的外部设备 ③Qt中的文件操作与外部设备的操作相同 (2)IO操 ...

  9. C++ 文件操作(CFile类)

    原文:文件操作(CFile),C吉羊 一.Visual C++编程文件操作 有如下方法可进行操作: (1)使用标准C运行库函数,包括fopen.fclose.fseek等. (2)使用Win16下的文 ...

随机推荐

  1. PHPSESSID的cookie

    如果PHP脚本中有: 1 session_start(); 则说明使用了SESSION. SESSION是一种机制,可以在服务器端跨文件暂时保存数据或传递数据,常用于购物车等方面. SESSION只在 ...

  2. 一维条形码攻击技术(Badbarcode)

    0x00 前言 在日常生活中,条形码随处可见,特别在超市,便利店,物流业,但你们扫的条形码真的安全吗?之前TK教主 在PacSec介绍的条形码攻击和twitter上的demo视频太炫酷,所以就自己买了 ...

  3. VS2013 JS 跟踪

    VS2013JS  跟踪 1.在页面对应的cs文件中设置断点,F10单步执行. 2.单步执行调试会自然而然执行到JS里面.

  4. 3、SQL基础整理(分组)

    分组 group by select class from xuesheng group by class select class,AVG(chinese)from xuesheng group b ...

  5. 农场阳光 (simpson)

    计算若干个圆与一个矩形的面积并 simpson公式 ans = ( f[l] + f[r] + 4 * f[mid] ) * (r-l) / 6 uses math; type arr=record ...

  6. 清除浮动2-父元素设置overflow:hidden

    <!doctype html><html> <head> <meta charset="UTF-8"> <meta name= ...

  7. HDU 1053 & HDU 2527 哈夫曼编码

    http://acm.hdu.edu.cn/showproblem.php?pid=1053 #include <iostream> #include <cstdio> #in ...

  8. magento 多域名多店铺

    事前指导 我们使用的就是parked domain ,将你要添加的域名指向你的现有magento 文件根目录. 就像预习中提到的,我们有个magento站域名为one.com ,添加新的域名two.c ...

  9. yii表单

    yii  的dropdownlist,用yii的session可以记下选中的状态 $form = $this->beginWidget('CActiveForm',array('action'= ...

  10. python字典中的元素类型

    python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...