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!\n");
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!\n");
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;
}