zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    windows下postgreSQL安装与启动
    Map、Set、List区别
    责任链模式实战
    Java的URL类(二)
    linux 之分区和文件系统
    linux 之网络命令
    linux 之用户管理
    linux 权限之acl
    我的hadoop学习之路
    Hadoop 学习之Docker
  • 原文地址:https://www.cnblogs.com/SmallAndGreat/p/12107519.html
Copyright © 2011-2022 走看看