1、C文件操作:
(1)头文件
#include <stdio.h>
(2)写(注释中的) 与 读:
int cFileOperate()
{
/* char szTem[] = "kaven";
int a = 48;
// C programing
FILE *fp = fopen("D://a.txt", "w");
if (fp != NULL)
{
int b = fwrite(&a, 1, sizeof(int), fp);
fwrite(szTem, 1, sizeof(szTem), fp);
fclose(fp);
}
*/
char szTem[10] = {0};
int a = 0;
FILE *fp = fopen( "D://a.txt" , "r" );
if (fp != NULL)
{
//fread(&a, 1, sizeof(int), fp);
fseek(fp, 4, SEEK_SET);
fread(szTem, 1, 20, fp);
fclose(fp);
}
return 0;
}
2、C++文件操作:
(1)头文件
#include <fstream>
using namespace std;
(2)写 与 读:
int cppFileOperate()
{
// char szTmp[] = "Hello";
// ofstream ofs;
// ofs.open("D://a.txt");
// ofs.write(szTmp, sizeof(szTmp));
// ofs.close();
char szTmp[20] = {0};
ifstream ifs;
ifs.open( "D://a.txt" );
ifs.read(szTmp, sizeof(szTmp));
ifs.close();
fstream fs;
return 0;
}
3、API文件操作:
(1)头文件:
#include <Windows.h>
(2)写 与 读:
int apiFileOperate()
{
// char szTmp[] = "Api File Operation";
// DWORD dw = 0;
//
// HANDLE hfile = CreateFile(_T("D://a.txt"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// WriteFile(hfile, szTmp, strlen(szTmp), &dw, NULL);
// CloseHandle(hfile);
char szTmp[128] = {0};
DWORD dw = 0;
HANDLE hfile = CreateFile(_T( "D://a.txt" ), GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
ReadFile(hfile, szTmp, 18, &dw, NULL);
CloseHandle(hfile);
return 0;
}