Windows 写文件

点击跳转到视频教程

使用的API: WriteFile

1.写入数字

#include<windows.h>
#include<tchar.h>
#include<iostream>
using namespace std;
int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE hPreInstance, LPTSTR lpCmdLine, int nShowCmd) {

	HANDLE hFile = CreateFile(_T("E:\\789.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile != INVALID_HANDLE_VALUE)
	{
		int num = 123;
		DWORD dwRealWrite = 0;
		BOOL bRet = WriteFile(hFile, &num, sizeof(int), &dwRealWrite, NULL);
		if (bRet)
		{
			MessageBox(NULL, _T("数据写入成功"), _T("tip"), MB_OK);
		}
		else
		{
			MessageBox(NULL, _T("数据写入失败"), _T("tip"), MB_OK);
		}
		// 只有打开成功的时候才关闭句柄,打开失败的时候hFile不需要关闭
		CloseHandle(hFile);
	}
	else
	{
		MessageBox(NULL, _T("打开文件失败"), _T("tip"), MB_OK);
	}
	return 0;
}

可以看到创建文件成功,打开文件发现是一个"{"字符,实际上使用16进制编辑器打开会看到16进制的数字7B,也就是二进制对应的十进制数字123
Windows 写文件

2.写入字符

#include<windows.h>
#include<tchar.h>
#include<iostream>
using namespace std;
int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE hPreInstance, LPTSTR lpCmdLine, int nShowCmd) {

	HANDLE hFile = CreateFile(_T("E:\\789.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile != INVALID_HANDLE_VALUE)
	{
		//int num = 123;
		char c = 'q';
		DWORD dwRealWrite = 0;
		//BOOL bRet = WriteFile(hFile, &num, sizeof(int), &dwRealWrite, NULL);
		BOOL bRet = WriteFile(hFile, &c, sizeof(char), &dwRealWrite, NULL);
		if (bRet)
		{
			MessageBox(NULL, _T("数据写入成功"), _T("tip"), MB_OK);
		}
		else
		{
			MessageBox(NULL, _T("数据写入失败"), _T("tip"), MB_OK);
		}
		// 只有打开成功的时候才关闭句柄,打开失败的时候hFile不需要关闭
		CloseHandle(hFile);
	}
	else
	{
		MessageBox(NULL, _T("打开文件失败"), _T("tip"), MB_OK);
	}
	return 0;
}

Windows 写文件
3.写入字符串

#include<windows.h>
#include<tchar.h>
#include<iostream>
using namespace std;
int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE hPreInstance, LPTSTR lpCmdLine, int nShowCmd) {

	HANDLE hFile = CreateFile(_T("E:\\789.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile != INVALID_HANDLE_VALUE)
	{
		int num = 123;
		char c = 'q';
		char str[] = "你好!世界";
		DWORD dwRealWrite = 0;
		//BOOL bRet = WriteFile(hFile, &num, sizeof(int), &dwRealWrite, NULL);
		//BOOL bRet = WriteFile(hFile, &c, sizeof(char), &dwRealWrite, NULL);
		BOOL bRet = WriteFile(hFile, str, sizeof(str), &dwRealWrite, NULL);
		if (bRet)
		{
			MessageBox(NULL, _T("数据写入成功"), _T("tip"), MB_OK);
		}
		else
		{
			MessageBox(NULL, _T("数据写入失败"), _T("tip"), MB_OK);
		}
		// 只有打开成功的时候才关闭句柄,打开失败的时候hFile不需要关闭
		CloseHandle(hFile);
	}
	else
	{
		MessageBox(NULL, _T("打开文件失败"), _T("tip"), MB_OK);
	}
	return 0;
}

Windows 写文件

4.写入二进制数据

#include<windows.h>
#include<tchar.h>
#include<iostream>
using namespace std;

struct Student {
	int age;
	char sex;
	char name[32];
};
int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE hPreInstance, LPTSTR lpCmdLine, int nShowCmd) {

	HANDLE hFile = CreateFile(_T("E:\\789.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile != INVALID_HANDLE_VALUE)
	{
		int num = 123;
		char c = 'q';
		char str[] = "你好!世界";
		DWORD dwRealWrite = 0;
		//BOOL bRet = WriteFile(hFile, &num, sizeof(int), &dwRealWrite, NULL);
		//BOOL bRet = WriteFile(hFile, &c, sizeof(char), &dwRealWrite, NULL);
		//BOOL bRet = WriteFile(hFile, str, sizeof(str), &dwRealWrite, NULL);
		Student student;
		student.age = 12;
		student.sex = 'n';
		strcpy(student.name, "hg");
		
		BOOL bRet = WriteFile(hFile, &student, sizeof(student), &dwRealWrite, NULL);
		if (bRet)
		{
			MessageBox(NULL, _T("数据写入成功"), _T("tip"), MB_OK);
		}
		else
		{
			MessageBox(NULL, _T("数据写入失败"), _T("tip"), MB_OK);
		}
		// 只有打开成功的时候才关闭句柄,打开失败的时候hFile不需要关闭
		CloseHandle(hFile);
	}
	else
	{
		MessageBox(NULL, _T("打开文件失败"), _T("tip"), MB_OK);
	}
	return 0;
}

Windows 写文件

上一篇:好用的终端


下一篇:如何将IP添加到Windows服务器的网络