Windows 文件指针使用

点击跳转到视频教程

使用的API: SetFilePointer
文件指针从文件开头开始移动
SetFilePointer(hFile, sizeof(int)+sizeof(char), NULL, FILE_BEGIN);

#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[32] = "你好!世界";
		Student student;
		student.age = 12;
		student.sex = 'n';
		strcpy(student.name, "hg");
		DWORD dwRealWrite = 0;
		WriteFile(hFile, &num, sizeof(int), &dwRealWrite, NULL);
		WriteFile(hFile, &c, sizeof(char), &dwRealWrite, NULL);
		WriteFile(hFile, str, sizeof(str), &dwRealWrite, NULL);
		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);

		/

		// 然后按刚才写入的顺序,把刚才写入的内容读取出来
		hFile = CreateFile(_T("E:\\789.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		DWORD dwRealRead = 0;
		int readInt;
		char readChar;
		char readstr[32] = { 0 };
		Student readStudent;
		if (hFile != INVALID_HANDLE_VALUE)
		{
			 //FILE_BEGIN 的意思是文件指针从文件头开始移动
			SetFilePointer(hFile, sizeof(int)+sizeof(char), NULL, FILE_BEGIN);
			
			/*ReadFile(hFile, &readInt, sizeof(int), &dwRealRead, NULL);
			ReadFile(hFile, &readChar, sizeof(char), &dwRealRead, NULL);*/
			ReadFile(hFile, readstr, sizeof(readstr), &dwRealRead, NULL);
			bRet = ReadFile(hFile, &readStudent, sizeof(readStudent), &dwRealRead, NULL);

			if (bRet)
			{
				MessageBox(NULL, _T("数据读取成功"), _T("tip"), MB_OK);
			}
			else
			{
				MessageBox(NULL, _T("数据读取失败"), _T("tip"), MB_OK);
			}
		}
	}
	else
	{
		MessageBox(NULL, _T("打开文件失败"), _T("tip"), MB_OK);
	}
	return 0;
}

在这里插入图片描述

文件指针从文件末尾开始移动
SetFilePointer(hFile, ((int)-32 - (int)sizeof(Student)), NULL, FILE_END);

#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[32] = "你好!世界";
		Student student;
		student.age = 12;
		student.sex = 'n';
		strcpy(student.name, "hg");
		DWORD dwRealWrite = 0;
		WriteFile(hFile, &num, sizeof(int), &dwRealWrite, NULL);
		WriteFile(hFile, &c, sizeof(char), &dwRealWrite, NULL);
		WriteFile(hFile, str, sizeof(str), &dwRealWrite, NULL);
		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);

		/

		// 然后按刚才写入的顺序,把刚才写入的内容读取出来
		hFile = CreateFile(_T("E:\\789.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		DWORD dwRealRead = 0;
		int readInt;
		char readChar;
		char readstr[32] = { 0 };
		Student readStudent;
		if (hFile != INVALID_HANDLE_VALUE)
		{
			// FILE_BEGIN 的意思是文件指针从文件头开始移动
			//SetFilePointer(hFile, sizeof(int)+sizeof(char), NULL, FILE_BEGIN);
			//
			///*ReadFile(hFile, &readInt, sizeof(int), &dwRealRead, NULL);
			//ReadFile(hFile, &readChar, sizeof(char), &dwRealRead, NULL);*/
			//ReadFile(hFile, readstr, sizeof(readstr), &dwRealRead, NULL);
			//bRet = ReadFile(hFile, &readStudent, sizeof(readStudent), &dwRealRead, NULL);

			SetFilePointer(hFile, ((int)-32 - (int)sizeof(Student)), NULL, FILE_END);
			ReadFile(hFile, readstr, sizeof(readstr), &dwRealRead, NULL);
			bRet = ReadFile(hFile, &readStudent, sizeof(readStudent), &dwRealRead, NULL);
			if (bRet)
			{
				MessageBox(NULL, _T("数据读取成功"), _T("tip"), MB_OK);
			}
			else
			{
				MessageBox(NULL, _T("数据读取失败"), _T("tip"), MB_OK);
			}
		}
	}
	else
	{
		MessageBox(NULL, _T("打开文件失败"), _T("tip"), MB_OK);
	}
	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值