{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/c-cpp-file-io/",
  "atlas": "https://www.pystone.net/?node=c-cpp-file-io#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/c-cpp-file-io.md",
  "context": "https://www.pystone.net/notes/c-cpp-file-io.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:c-cpp-file-io",
  "slug": "c-cpp-file-io",
  "title": "C与C++文件操作",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "C与C++文件操作",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "3f87f908e3f67ef0eb77c8f055565597e188fbef562f1a38b3f8133e85743502",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "C与C++文件操作",
      "anchor": "c与c文件操作",
      "citation": "https://www.pystone.net/notes/c-cpp-file-io/#c%E4%B8%8Ec%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C"
    },
    {
      "depth": 2,
      "text": "cc++文件操作",
      "anchor": "cc文件操作",
      "citation": "https://www.pystone.net/notes/c-cpp-file-io/#cc%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C"
    },
    {
      "depth": 3,
      "text": "流式文件操作",
      "anchor": "流式文件操作",
      "citation": "https://www.pystone.net/notes/c-cpp-file-io/#%E6%B5%81%E5%BC%8F%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C"
    },
    {
      "depth": 3,
      "text": "直接I/O文件操作",
      "anchor": "直接io文件操作",
      "citation": "https://www.pystone.net/notes/c-cpp-file-io/#%E7%9B%B4%E6%8E%A5io%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C"
    },
    {
      "depth": 2,
      "text": "C++",
      "anchor": "c",
      "citation": "https://www.pystone.net/notes/c-cpp-file-io/#c"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:programming-languages-and-runtimes",
      "title": "编程语言与运行时",
      "url": "https://www.pystone.net/notes/programming-languages-and-runtimes/",
      "atlas": "https://www.pystone.net/?node=programming-languages-and-runtimes#knowledge-atlas",
      "label": "编程语言与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "C++中的“C与C++文件操作”导航项",
      "citation": "https://www.pystone.net/notes/programming-languages-and-runtimes/#c"
    }
  ],
  "contentMarkdown": "# C与C++文件操作\n\n> 创建时间：2020/3/11 1:27\n\n## cc++文件操作\n### 流式文件操作\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n> fopen() 打开流\n>  fclose() 关闭流\n>  fputc() 写一个字符到流中\n>  fgetc() 从流中读一个字符\n>  fseek() 在流中定位到指定的字符\n>  fputs() 写字符串到流\n>  fgets() 从流中读一行或指定个字符\n>  fprintf() 按格式输出到流\n>  fscanf() 从流中按格式读取\n>  feof() 到达文件尾时返回真值\n>  ferror() 发生错误时返回其值\n>  rewind() 复位文件定位器到文件开始处\n>  remove() 删除文件\n>  fread() 从流中读指定个数的字符\n>  fwrite() 向流中写指定个数的字符\n>  tmpfile() 生成一个临时文件流\n>  tmpnam() 生成一个唯一的文件名\n\n  1. FILE *fopen(const char *filename,const char *mode)，fopen实现三个功能\n\"r\" 以只读方式打开文件\n\"w\" 以只写方式打开文件\n\"a\" 以追加方式打开文件\n\"r+\" 以读/写方式打开文件，如无文件出错\n\"w+\" 以读/写方式打开文件，如无文件生成新文件\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n  2. int fclose(FILE *fp);\n如果成功，返回0,失败返回EOF\n在程序结束时一定要记得关闭打开的文件，不然可能会造成数据丢失的情况，我以前就经常犯这样的毛病。\n  3. int fputc(int c, FILE *stream);\n  4. int fputc(FILE *stream);\n  5. int fseek(FILE *stream, long offset, int whence);\n一般用于二进制模式打开的文件中，功能是定位到流中指定的位置\n参数offset是移动的字符数，whence是移动的基准，取值是\n符号常量 值 基准位置\nSEEK_SET 0 文件开头\nSEEK_CUR 1 当前读写的位置\nSEEK_END 2 文件尾部\n  6. int fputs(const char *s, FILE *stream);\n\n> 详细参考：https://blog.csdn.net/hbqjzx/article/details/86525693\n\n### 直接I/O文件操作\n\n通过直接存/取文件来完成对文件的处理，而上篇所说流式文件操作是通过缓冲区来进行；流式文件操作是围绕一个FILE指针来进行，而此类文件操作是围绕一个文件的\"句柄\"来进行，什么是句柄呢？它是一个整数，是系统用来标识一个文件(在WINDOWS中，句柄的概念扩展到所有设备资源的标识)的唯一的记号。\n这些函数及其所用的一些符号在 **io.h** 和 **fcntl.h** 中定义\n\n> open() 打开一个文件并返回它的句柄\n>  close() 关闭一个句柄\n>  lseek() 定位到文件的指定位置\n>  read() 块读文件\n>  write() 块写文件\n>  eof() 测试文件是否结束\n>  filelength() 取得文件长度\n>  rename() 重命名文件\n>  chsize() 改变文件长度\n\n> 详细参考：https://blog.csdn.net/hbqjzx/article/details/86525693\n\n## C++\n"
}
