使用的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;
}