标准C方式写文件:
int main ()
{
FILE *fp;
fp = fopen("a.bin", "wb"); // 以二进制的方式写
if (fp == NULL)
{
printf("Can not open file a.txt");
}
char c = 'c';
char b = 0x12;
int i = 1;
char buffer[10];
strcpy(buffer, "hello");
/* 写入一个字节 */
fwrite(&c, 1, 1, fp);
/* 写入一个字节 */
fwrite(&b, 1, 1, fp);
/* 写入四个字节 */
fwrite(&i, 4, 1, fp);
/* 写入5个字节 */
fwrite(buffer, 5, 1, fp);
fclose(fp);
return 0;
}
标准C方式读文件:
int main ()
{
FILE *fp;
fp = fopen("a.txt", "rb"); // 以二进制的方式读
if (fp == NULL)
{
printf("Can not open file a.txt");
}
char c;
int i;
char buffer[10];
/* 读入一个字节 */
fread(&c, 1, 1, fp);
printf("c: %c/n", c);
/* 读入一个字节 */
fread(&b, 1, 1, fp);
printf("b: %.2x/n", b);
/* 读入4个字节 */
fread(&i, 4, 1, fp);
printf("i: %d/n", i);
/* 读入5个字节 */
fread(buffer, 5, 1, fp);
buffer[5] = '/0';
printf("buffer: %s/n", buffer);
fclose(fp);
return 0;
}
windows API方式写文件:
#include "windows.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// 创建文件
HANDLE hFile = ::CreateFile("a.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
OutputDebugString("Can not open file a.txt");
return 0;
}
char c = 'c';
char b = 0x12;
int i = 1;
char buffer[10];
strcpy(buffer, "hello");
DWORD dwWriteSize;
/* 写入一个字节 */
WriteFile(hFile, &c, 1, &dwWriteSize, NULL);
/* 写入一个字节 */
WriteFile(hFile, &b, 1, &dwWriteSize, NULL);
/* 写入四个字节 */
WriteFile(hFile, &i, 4, &dwWriteSize, NULL);
/* 写入五个字节 */
WriteFile(hFile, buffer, 5, &dwWriteSize, NULL);
/* 强制写入磁盘 */
FlushFileBuffers(hFile);
/*关闭文件*/
CloseHandle(hFile);
return 0;
}
windows API方式读文件:
#include "windows.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
HANDLE hFile;
hFile = ::CreateFile("a.txt",GENERIC_READ, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Can not open file a.txt");
return 0;
}
char c;
char b;
int i;
char buffer[10];
DWORD dwWriteSize;
/* 读入一个字节 */
ReadFile(hFile, &c, 1, &dwWriteSize, NULL);
printf("c: %c/n", c);
/* 读入一个字节 */
ReadFile(hFile, &b, 1, &dwWriteSize, NULL);
printf("b: %.2x/n", b);
/* 读入四个字节 */
ReadFile(hFile, &i, 4, &dwWriteSize, NULL);
printf("i: %d/n", i);
/* 读入五个字节 */
ReadFile(hFile, buffer, 5, &dwWriteSize, NULL);
buffer[5] = '/0';
printf("buffer: %s/n", buffer);
/* 关闭文件 */
CloseHandle(hFile);
return 0;
}
标准C++方式写文件:
using namespace std;
int main(int argc, char* argv[])
{
ofstream out;
out.open("a.txt", ios::binary);
char c = 'c';
char b = 0x12;
int i = 1;
char buffer[10];
strcpy(buffer, "hello");
/* 写入一个字节 */
out.write(&c, 1);
/* 写入一个字节 */
out.write(&b, 1);
/* 写入四个字节 */
out.write((char*)(&i), 4);
/* 写入五个字节 */
out.write(buffer, 5);
out.flush();
out.close();
return 0;
}
标准C++方式写文件:
using namespace std;
int main(int argc, char* argv[])
{
ifstream in;
in.open("a.txt", ios::binary);
char c;
char b;
int i;
char buffer[10];
in.read(&c, 1);
printf("c: %c/n", c);
in.read(&b, 1);
printf("b: %.2x/n", b);
in.read((char*)(&i), 4);
printf("i: %d/n", i);
in.read(buffer, 5);
buffer[5] = '/0';
printf("buffer: %s/n", buffer);
return 0;
}
MFC方式写文件:
file.Open("a.txt", CFile::modeWrite|CFile::modeCreate);
char c = 'c';
char b = 0x12;
int i = 1;
char buffer[10];
strcpy(buffer, "hello");
/* 写入1字节 */
file.Write(&c, 1);
/* 写入1字节 */
file.Write(&b, 1);
/* 写入4字节 */
file.Write(&i, 4);
/* 写入5字节 */
file.Write(buffer, 5);
file.Flush();
file.Close();
MFC方式读文件:
file.Open("a.txt", CFile::modeRead);
char c;
char b;
int i;
char buffer[10];
CString forPrint;
/* 读入1字节 */
file.Read(&c, 1);
forPrint.Format("c: %c", c);
MessageBox(forPrint);
/* 读入1字节 */
file.Read(&b, 1);
forPrint.Format("b: %.2x", b);
MessageBox(forPrint);
/* 读入4字节 */
file.Read(&i, 4);
forPrint.Format("i: %d", i);
MessageBox(forPrint);
/* 读入5字节 */
file.Read(buffer, 5);
buffer[5] = '/0';
forPrint.Format("buffer: %s", buffer);
MessageBox(forPrint);
file.Close();