Windows 写文件

2022/2/27 7:28:28

本文主要是介绍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
在这里插入图片描述

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;
}

在这里插入图片描述
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;
}

在这里插入图片描述

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 写文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程