• C语言读写文件


    #include <stdio.h>

    #include <stdlib.h>

    int ReadFile(char* str, int len, const char* path)
    {
      FILE* pFile;
      fopen_s(&pFile, path, "rb+");
      if (NULL == pFile)
      {
        printf("File Open Failed! ");
        return -1;
      }

      fread(str, 1, len, pFile);

      fclose(pFile);
      return 0;
    }

    int WriteFile(char* str, int len, const char* path)
    {
      FILE* pFile;
      fopen_s(&pFile, path, "wb+");
      if (NULL == pFile)
      {
        printf("File Open Failed! ");
        return -1;
      }

      fwrite(str, 1, len, pFile);

      fclose(pFile);
      return 0;
    }

    int main()
    {
      const char* sPath = "C:/Users/GHL/Desktop/GHL.txt";
      char sRead[100] = {0};
      char sWrite[100] = "ggggggggggggggggggggggggggg";

      int nRead = _countof(sRead);
      int nWrite = _countof(sWrite);

      WriteFile(sWrite, nWrite, sPath);
      ReadFile(sRead, nRead, sPath);

      return 0;
    }

  • 相关阅读:
    【20170923】2017暑假北京学习 day 3
    Leetcode 39
    事无巨细 | 访问一个网站的过程
    Leetcode 77
    排序 | 快速排序
    排序 | 堆排序
    Leetcode 60
    Leetcode 51
    海量数据处理的解法
    Leetcode 99
  • 原文地址:https://www.cnblogs.com/SmallAndGreat/p/12107519.html
Copyright © 2020-2023  润新知