windows系统磁盘文件存储:
分区格式:NTFS / FAT32
GetSystemDirectory();//获取系统路径GetWindowsDirectory();//获取windows路径
GetCurrentDirectory();//获取当前工作目录
SetCurrentDirectory();//修改当前工作目录
CreateFile/ReadFile/WriteFile/GetFileSize/SetFilePointer
//返回文件大小的低32位
DWORD GetFileSize( HANDLE hFile,// handle to file
LPDWORD lpFileSizeHigh //输出参数:返回文件大小的高32位
);
//创建文件并写入文本代码示例
HANDLE hf=CreateFile("file.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,
OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
char txt[]="hello file to write";
DWORD len=0;//返回实际写入的长度
WriteFile(hf,txt,strlen(txt),&len,NULL);
CloseHandle(hf);
//读取代码示例
HANDLE hf=CreateFile("file.txt",GENERIC_READ,FILE_SHARE_READ,
NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
CHAR txt[1000]={0};
DWORD len=0;//实际读到的长度
ReadFile(hf,txt,sizeof(txt),&len,NULL);
printf("读取到的字符串:%s\n",txt);
CloseHandle(hf);
day75 am over!
创建目录:CreateDirectory
移动目录(不能跨分区移动)、移动文件(可以跨分区):
BOOL MoveFile( LPCTSTR lpExistingFileName,// file name
LPCTSTR lpNewFileName// new file name
);
删除空目录:RemoveDirectory
复制文件:CopyFile
删除文件:DeleteFile
//查找文件,返回查找句柄
HANDLE FindFirstFile( LPCTSTR lpFileName, // file name
LPWIN32_FIND_DATA lpFindFileData //输出参数 data buffer
);
//查找下一个
BOOL FindNextFile( HANDLE hFindFile, // search handle
LPWIN32_FIND_DATA lpFindFileData // data buffer
);
//关闭查找句柄
BOOL FindClose( HANDLE hFindFile // file search handle
);
//使用示例
char *path="c:/*.*";
WIN32_FIND_DATA wfd={0};
HANDLE hf=FindFirstFile(path,&wfd);
while(1)
{
if(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
printf("目录:[%s]\n",wfd.cFileName);
}
else
{
printf("文件:%s\n",wfd.cFileName);
}
if(FindNextFile(hf,&wfd)==0) break;
}
if(hf) FindClose(hf);
示例代码:
// winlogic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<STDIO.H> #include<WINDOWS.H> void GetLogical() { UINT logic=GetLogicalDrives(); printf("%d\n",logic); } void GetPath() { char txt[200]={0}; GetSystemDirectory(txt,sizeof(txt)); printf("系统路径%s\n",txt); memset(txt,0,sizeof(txt)); GetWindowsDirectory(txt,sizeof(txt)); printf("windows路径%s\n",txt); memset(txt,0,sizeof(txt)); GetCurrentDirectory(sizeof(txt),txt); printf("程序工作路径%s\n",txt); SetCurrentDirectory("c:/"); memset(txt,0,sizeof(txt)); GetCurrentDirectory(sizeof(txt),txt); printf("更改后程序工作路径%s\n",txt); } void CreateWrite() { HANDLE hf=CreateFile("file.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL, OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); char txt[]="hello file to write"; DWORD len=0;//返回实际写入的长度 WriteFile(hf,txt,strlen(txt),&len,NULL); CloseHandle(hf); } void Read() { HANDLE hf=CreateFile("file.txt",GENERIC_READ,FILE_SHARE_READ, NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); CHAR txt[1000]={0}; DWORD len=0;//实际读到的长度 ReadFile(hf,txt,sizeof(txt),&len,NULL); printf("读取到的字符串:%s\n",txt); CloseHandle(hf); } void CreateDir() { CreateDirectory("abc/",NULL); } void Find() { char *path="c:/*.*"; WIN32_FIND_DATA wfd={0}; HANDLE hf=FindFirstFile(path,&wfd); while(1) { if(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { printf("目录:[%s]\n",wfd.cFileName); } else { printf("文件:%s\n",wfd.cFileName); } if(FindNextFile(hf,&wfd)==0) break; } if(hf) FindClose(hf); } int main(int argc, char* argv[]) { // GetLogical(); // GetPath(); //CreateWrite(); //Read(); //CreateDir(); Find(); char c=0; printf("按任意键结束!\n"); scanf("%c",&c); return 0; }
windows内存:
1、区域(针对内存的概念):一般是64K的倍数。
空闲:没被使用
私有:被预定
映像:存放代码
映射:存放数据
2、物理内存
系统可以使用的实际内存,CPU可以直接访问的内存
3、虚拟内存
将硬盘文件虚拟成内在使用(pagafile.sys),CPU如果要访问虚拟内存数据,必须将虚拟内存数据放到物理内存。
4、内存页(针对地址的概念)
系统管理内存的最小单位。内存页大小为4K,每内存页有自己的权限。
5、页目表。
指针地址(32位):
31~22 21~12 11~0
页目(1K) 页表(1K) 页内偏移地址(4K)
6、从内存获取数据的过程
1、先在物理内存中找;
2、找不到时在虚拟内在中找;
3、如果在虚拟内存中找到,把物理内存与虚拟内存页数据交换;
4、使用物理内存数据;
5、最后可能物理内存数据与虚拟内存数据交换还原。
7、内存分配
7.1 虚拟内存分配-适合大内存分配,一般是1M之上的内存。
7.2 堆内存分配-适合小内存分配,一般是1M以下的内存。
7.3 栈内存分配-适合小内存分配,一般是1M以下的内存。
//虚拟内存分配函数,分配成功后返回地址
LPVOID VirtualAlloc( LPVOID lpAddress,//NULL或提交地址
SIZE_T dwSize, //分配大小
DWORD flAllocationType, //分配方式
DWORD flProtect//内存访问方式
);
flAllocationType:
MEM_COMMIT 分配之后返回地址和内存空间
MEM_RESERVE 保留地址,分配之后只返回地址,内存空间不生成
flProtect:一般 PAGE_READWRITE
//查看内存的使用情况
VOID GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer
);
typedef struct _MEMORYSTATUS {
DWORD dwLength;
DWORD dwMemoryLoad;
DWORD dwTotalPhys;//物理内存总大小
DWORD dwAvailPhys;//物理内存可用大小
DWORD dwTotalPageFile;//虚拟内存总大小
DWORD dwAvailPageFile;//虚拟内存可用大小
DWORD dwTotalVirtual;//地址总数
DWORD dwAvailVirtual;//可用地址数
} MEMORYSTATUS, *LPMEMORYSTATUS;
BOOL VirtualFree( LPVOID lpAddress,//释放地址
SIZE_T dwSize, // 释放大小
DWORD dwFreeType // 释放方式
);
dwFreeType:
MEM_DECOMMIT-只释放内存
MEM_RELEASE -释放内存和地址
代码示例:
void ShowMemStatus()
{
MEMORYSTATUS mm;
mm.dwLength=sizeof(mm);
GlobalMemoryStatus(&mm);
printf("内存使用率:%d/100\n",mm.dwMemoryLoad);
printf("TotalPhys:%u MB\n",mm.dwTotalPhys/1024/1024);
printf("AvaliPhys:%u MB\n",mm.dwAvailPhys/1024/1024);
printf("TotalVirtual:%u MB\n",mm.dwTotalPageFile/1024/1024);
printf("AvaliVirtual:%u MB\n",mm.dwAvailPageFile/1024/1024);
printf("TotalAddr:%u MB\n",mm.dwTotalVirtual/1024/1024);
printf("AvaliAddr:%u MB\n",mm.dwAvailVirtual/1024/1024);
}
void Virtual_info()
{
ShowMemStatus();
char* p=(char*)VirtualAlloc(NULL,1024*1024*1024,MEM_COMMIT,PAGE_READWRITE);//1G内存和地址
ShowMemStatus();
VirtualFree(p,0,MEM_RELEASE);
ShowMemStatus();
}
void Virtual_commit()
{
char* p=(char*) VirtualAlloc(NULL,4096,MEM_COMMIT,PAGE_READWRITE);
strcpy(p,"HELLO MEMORY");
printf("%s\n",p);
VirtualFree(p,0,MEM_RELEASE);
}
void Virtual_reserve()
{
//申请地址,不分配内存
char* p =(char*) VirtualAlloc(NULL,1024*1024,MEM_RESERVE,PAGE_READWRITE);
char* p1=(char*)VirtualAlloc(p,4096,MEM_COMMIT,PAGE_READWRITE);//提交内存,以一个内存页4k为最小单位
strcpy(p,"HELLO MEMORY");
printf("%s\n",p);
VirtualFree(p,0,MEM_RELEASE);
}
堆内存Heap
GetProcessHeap-获取程序默认第一个堆
GetProcessHeaps-获取程序所有默认堆
示例:
HANDLE h1=GetProcessHeap();
HANDLE hs[256]={0};
DWORD count=GetProcessHeaps(256,hs);
HeapCreate -创建堆
HeapAlloc -从堆分配内存
-把内存还给堆
HeapDestroy -销毁堆
示例:
HANDLE h= HeapCreate(HEAP_NO_SERIALIZE,1024*1024,0);
char* p= (char*) HeapAlloc(h,HEAP_ZERO_MEMORY,2*1024*1024);
strcpy(p,"123456");
printf("%s\n",p);
HeapFree(h,0,p);
HeapDestroy(h);
如果要使用,建议用:malloc/new 和 free/delete
内存映射文件的操作,速度快:
1、先创建一个硬盘文件 CreateFile()
2、创建内存映射文件
HANDLE CreateFileMapping( HANDLE hFile, // 硬盘文件的句柄
LPSECURITY_ATTRIBUTES lpAttributes, // 传0就行
DWORD flProtect, // protection
DWORD dwMaximumSizeHigh, // 高32位大小,小于4G传0
DWORD dwMaximumSizeLow, // 低32位大小
LPCTSTR lpName // 命名,可以为NULL
);
3、获取映射文件中某部分的地址
LPVOID MapViewOfFile(
HANDLE hFileMappingObject, // 映射文件句柄
DWORD dwDesiredAccess, // 访问权限
DWORD dwFileOffsetHigh, // 高32位大小,小于4G传0
DWORD dwFileOffsetLow, // 低32位大小
SIZE_T dwNumberOfBytesToMap // 映射的字节数量
);
dwFileOffsetHigh和dwFileOffsetLow 的合成必须是64K的整数倍。
4、断开地址和映射文件某部分
BOOL UnmapViewOfFile( LPCVOID lpBaseAddress//
);
5、关闭映射文件
CloseHandle(HANDLE)
6、关闭硬盘文件
CloseHandle(HANDLE);
示例:
HANDLE file=CreateFile("file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,
0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
HANDLE map=CreateFileMapping(file,0,PAGE_READWRITE,0,1024*1024,"pkm");
//
char* p=(char*)MapViewOfFile(map,FILE_MAP_ALL_ACCESS,0,64*1024,0);
strcpy(p,"adsadsad asd as d\n as d a\n s d as d a s aaaaa bbbbb ccccc\n");
printf("%s\n",p);
UnmapViewOfFile(p);
getchar();
CloseHandle(map);//关闭就没了,如果其他进程要使用,则不能关。
CloseHandle(file);//关闭了还在硬盘上
其他进程获取本进程已经创建好的映射文件,不能提前关闭映射文件
HANDLE OpenFileMapping( DWORD dwDesiredAccess, // access mode
BOOL bInheritHandle, // 返回值能否被子进程使用
LPCTSTR lpName // 映射名称
);
示例:
HANDLE map=OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"pkm");
char* p=(char*)MapViewOfFile(map,FILE_MAP_ALL_ACCESS,0,64*1024,0);
printf("%s\n",p);
UnmapViewOfFile(p);
示例代码:
// winVirtual.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<STDIO.H> #include <WINDOWS.H> void ShowMemStatus() { MEMORYSTATUS mm; mm.dwLength=sizeof(mm); GlobalMemoryStatus(&mm); printf("内存使用率:%d/100\n",mm.dwMemoryLoad); printf("TotalPhys:%u MB\n",mm.dwTotalPhys/1024/1024); printf("AvaliPhys:%u MB\n",mm.dwAvailPhys/1024/1024); printf("TotalVirtual:%u MB\n",mm.dwTotalPageFile/1024/1024); printf("AvaliVirtual:%u MB\n",mm.dwAvailPageFile/1024/1024); printf("TotalAddr:%u MB\n",mm.dwTotalVirtual/1024/1024); printf("AvaliAddr:%u MB\n",mm.dwAvailVirtual/1024/1024); } void Virtual_info() { ShowMemStatus(); char* p=(char*)VirtualAlloc(NULL,1024*1024*1024,MEM_COMMIT,PAGE_READWRITE); ShowMemStatus(); VirtualFree(p,0,MEM_RELEASE); ShowMemStatus(); } void Virtual_commit() { char* p=(char*) VirtualAlloc(NULL,4096,MEM_COMMIT,PAGE_READWRITE); strcpy(p,"HELLO MEMORY"); printf("%s\n",p); VirtualFree(p,0,MEM_RELEASE); } void Virtual_reserve() { //申请地址,不分配内存 char* p =(char*) VirtualAlloc(NULL,1024*1024,MEM_RESERVE,PAGE_READWRITE); char* p1=(char*)VirtualAlloc(p,4096,MEM_COMMIT,PAGE_READWRITE);//提交内存,以一个内存页4k为最小单位 strcpy(p,"HELLO MEMORY"); printf("%s\n",p); VirtualFree(p,0,MEM_RELEASE); } void HeapInfo() { HANDLE h1=GetProcessHeap(); printf("h1-%d\n",h1); HANDLE hs[256]={0}; DWORD count=GetProcessHeaps(256,hs); for(int i=0;i<count;i++) { printf("%d-%d\n",i,hs[i]); } } void MyHead() { HANDLE h= HeapCreate(HEAP_NO_SERIALIZE,1024*1024,0); char* p= (char*) HeapAlloc(h,HEAP_ZERO_MEMORY,2*1024*1024); strcpy(p,"123456"); printf("%s\n",p); HeapFree(h,0,p); HeapDestroy(h); } void FileMap() { HANDLE file=CreateFile("file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ, 0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); HANDLE map=CreateFileMapping(file,0,PAGE_READWRITE,0,1024*1024,"pkm"); // char* p=(char*)MapViewOfFile(map,FILE_MAP_ALL_ACCESS,0,64*1024,0); strcpy(p,"adsadsad asd as d\n as d a\n s d as d a s aaaaa bbbbb ccccc\n"); printf("%s\n",p); UnmapViewOfFile(p); getchar(); CloseHandle(map);//关闭就没了,如果其他进程要使用,则不能关。 CloseHandle(file);//关闭了还在硬盘上 } void ReadFileMap() { HANDLE map=OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"pkm"); char* p=(char*)MapViewOfFile(map,FILE_MAP_ALL_ACCESS,0,64*1024,0); printf("%s\n",p); UnmapViewOfFile(p); CloseHandle(map);//关闭就没了 } int main(int argc, char* argv[]) { //Virtual_info(); //Virtual_commit(); //Virtual_reserve(); //HeapInfo(); //MyHead(); FileMap(); //ReadFileMap(); return 0; }