Wiodows 系统编程实验报告文件编程Word文档格式.docx
《Wiodows 系统编程实验报告文件编程Word文档格式.docx》由会员分享,可在线阅读,更多相关《Wiodows 系统编程实验报告文件编程Word文档格式.docx(29页珍藏版)》请在冰豆网上搜索。
![Wiodows 系统编程实验报告文件编程Word文档格式.docx](https://file1.bdocx.com/fileroot1/2023-1/29/0bae4f9c-3e45-4300-834e-369e3aca5a6d/0bae4f9c-3e45-4300-834e-369e3aca5a6d1.gif)
(5)练习目录编程。
(6)练习使用选择文件的对话框。
(7)练习使用标准输入输出库读写文件。
(8)练习使用WindowsAPI操作文件。
(9)练习使用Cfile类操作文件。
(10)练习使用CfileFind类。
三、使用环境
Windows7MicrosoftVisualStudio
四、核心代码及调试过程
(1)练习获取当前系统中的逻辑磁盘驱动器:
在CselectDiskDriveDlg:
:
OnInitDialog()中添加如下代码:
size_talldrive=:
GetLogicalDriveStrings(0,NULL);
_TCHAR*driverstr;
driverstr=new_TCHAR[alldrive+sizeof(_T("
"
))];
size_ttwosize=GetLogicalDriveStrings(alldrive,driverstr);
CStringDriveLetter;
for(inti=0;
i<
alldrive/4;
i++)
{
DriveLetter=driverstr+i*4;
m_drive.InsertString(i,DriveLetter.GetBuffer());
}
m_drive.SetCurSel(0);
结果:
(2)练习获取磁盘驱动器类型:
voidCSelectDiskDriverDlg:
OnCbnSelchangeCombo1()
{
//TODO:
在此添加控件通知处理程序代码
CStringstrDrive;
m_drive.GetWindowTextA(strDrive);
intretCode=:
GetDriveType(strDrive.GetBuffer());
switch(retCode)
caseDRIVE_UNKNOWN:
m_DriveType="
驱动器类型:
未知"
;
break;
caseDRIVE_NO_ROOT_DIR:
m_DriveType="
驱动器根路径错误"
caseDRIVE_REMOVABLE:
可移动磁盘驱动器"
caseDRIVE_FIXED:
固定磁盘驱动器"
caseDRIVE_REMOTE:
网络磁盘驱动器"
caseDRIVE_CDROM:
CD—RAM"
caseDRIVE_RAMDISK:
RAM"
default:
break;
UpdateData(FALSE);
}
(3)练习获取磁盘的空间信息:
在CSelectDiskDriverDlg:
OnCbnSelchangeCombo1()继续添加如下获取磁盘空间的信息的代码:
ULARGE_INTEGERFreeAv,TotalBytes,FreeBytes;
if(GetDiskFreeSpaceEx(strDrive.GetBuffer(),&
FreeAv,&
TotalBytes,&
FreeBytes))
m_TotalSpace.Format("
总空间:
%s"
FormatSize(TotalBytes.QuadPart));
m_FreeSpace.Format("
剩余空间:
FormatSize(FreeBytes.QuadPart));
else
m_TotalSpace="
"
m_FreeSpace="
CStringCSelectDiskDriverDlg:
FormatSize(ULONGLONGsize)
CStringstrSize;
if(size>
1024)
if(size>
1024*1024)
{
strSize.Format("
%dMB"
size/1024/1024);
}
else
%dKB"
size/1024);
strSize.Format("
%dB"
size);
returnstrSize;
(4)练习使用选择目录的对话框:
intCALLBACKBrowserCallbackProc(HWNDhWnd,UINTuMsg,LPARAMlParam,LPARAMlpData)
CWndcw;
switch(uMsg)
caseBFFM_INITIALIZED:
:
SendMessage(hWnd,BFFM_SETSELECTION,1,lpData);
cw.Attach(hWnd);
cw.CenterWindow();
cw.Detach();
default:
return0;
voidCSelectFolderDlg:
OnBnClickedOk()
UpdateData(TRUE);
BROWSEINFObi;
ZeroMemory(&
bi,sizeof(BROWSEINFO));
LPMALLOCpMalloc;
bi.lParam=(LPARAM)m_path.GetBuffer();
bi.hwndOwner=NULL;
bi.lpfn=BrowserCallbackProc;
LPITEMIDLISTpidl=SHBrowseForFolder(&
bi);
TCHAR*path=newTCHAR[MAX_PATH];
if(pidl!
=NULL);
SHGetPathFromIDList(pidl,path);
m_path=path;
if(SUCCEEDED(SHGetMalloc(&
pMalloc)))
pMalloc->
Free(pidl);
Release();
deletepath;
(5)练习目录编程----
(1)创建目录:
#include"
stdafx.h"
//必须修改程序属性,将字符集设置为:
“未设置”。
#include<
Windows.h>
stdio.h>
int_tmain(intargc,_TCHAR*argv[])
charszDirPath[]="
d:
\\MyApp\\"
if(!
CreateDirectory(szDirPath,NULL))
printf("
Couldnotcreatenewdirectoty.\n"
);
else
printf("
Createnewdirectorysuccess.\n"
system("
pause"
练习目录编程----
(2)删除目录:
#include"
//if(!
RemoveDirectory(szDirPath))//只能传递一个参数
Couldnotdeletethedirectoty.\n"
Deletethedirectorysuccess.\n"
输出:
练习目录编程----(3)判断目录是否存在
BOOLFolderExist(LPCSTRstrPath)
WIN32_FIND_DATAwfd;
BOOLrValue=FALSE;
HANDLEhFind=FindFirstFile(strPath,&
wfd);
if((hFind!
=INVALID_HANDLE_VALUE)&
&
(wfd.dwFileAttributes&
FILE_ATTRIBUTE_DIRECTORY))
rValue=TRUE;
FindClose(hFind);
returnrValue;
if(FolderExist("
\\MyApp"
))//注:
不能为d:
\\MyApp\\
\MyAppisexisted.\n"
\MyApppisnotexisted.\n"
(6)文件编程----
(1)选择文件的方法:
voidCSelectFileDlg:
CFileDialogfileDlg(TRUE,"
*.txt"
m_file.GetBuffer(),OFN_HIDEREADONLY,"
文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
if(fileDlg.DoModal()==IDOK)
m_file=fileDlg.GetPathName();
输出:
文件编程----
(2)读取选择文件内容的功能:
FILE*stream;
char*pBuf;
if((stream=fopen(m_file,"
r+t"
))!
=NULL)
fseek(stream,0,SEEK_END);
intlength=ftell(stream);
pBuf=newchar[length];
rewind(stream);
fread(pBuf,sizeof(char),length,stream);
pBuf[length]='
\0'
fclose(stream);
MessageBox(pBuf);
MessageBox("
无法打开文件!
文件编程----(3)向文件中写入数据:
Windows.h"
FILE*stream;
charlist[30];
inti,numread,numwritten;
if((stream=fopen("
C:
\\test.txt"
"
w+t"
))!
numwritten=fwrite("
Helloworld."
sizeof(char),sizeof("
HelloWorld."
)/sizeof(char),stream);
写入数据量:
%d\n"
numwritten);
system("
(8)练习使用WindowsAPI操作文件
1.创建和打开文件:
HANDLEhFile;
hFile=CreateFile("
D:
\\myfile.txt"
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile==INVALID_HANDLE_VALUE)
Couldnotcreatefile(error%d)\n"
GetLastError());
return-1;
printf("
Createfilesuccessfully\n"
system("
2.获取文件的大小:
HANDLEhFile=INVALID_HANDLE_VALUE;
GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_RANDOM_ACCESS,NULL);
if(hFile==INVALID_HANDLE_VALUE)//注:
括号不要打在上面
printf("
Openfileerror\n"
returnfalse;
_LARGE_INTEGERliFileLen;
liFileLen.LowPart=:
GetFileSize(hFile,(DWORD*)&
(liFileLen.HighPart));
if(liFileLen.LowPart==0xFFFFFFFF)
GetFileSizeerror\n"
sizeoffile:
%d\n"
liFileLen.LowPart);
CloseHandle(hFile);
3.获取文件时间:
FILETIMEcreateTime;
FILETIMElastAccessTime;
FILETIMElastWriteTime;
if(GetFileTime(hFile,&
createTime,&
lastAccessTime,&
lastWriteTime))
SYSTEMTIMEstCreatetime,stLastAccessTime,stLastWriteTime;
memset(&
stCreatetime,0x0,sizeof(stCreatetime));
stLastAccessTime,0x0,sizeof(stLastAccessTime));
stLastWriteTime,0x0,sizeof(stLastWriteTime));
FileTimeToSystemTime(&
stCreatetime);
stLastAccessTime);
lastWriteTime,&
stLastWriteTime);
TIME_ZONE_INFORMATIONtz;
GetTimeZoneInformation(&
tz);
SYSTEMTIMElocalSTCreateTime,localSTlastAccessTime,localSTLastWriteTime;
SystemTimeToTzSpecificLocalTime(&
tz,&
stCreatetime,&
localSTCreateTime);
创建时间为:
%d-%d_%d%d:
%d:
%d\n"
localSTCreateTime.wYear,localSTCreateTime.wMonth,localSTCreateTime.wDay,
localSTCreateTime.wHour,localSTCreateTime.wMinute,localSTCreateTime.wSecond);
:
stLastAccessTime,&
localSTlastAccessTime);
最后访问时间:
localSTlastAccessTime.wYear,localSTlastAccessTime.wMonth,localSTlastAccessTime.wDay,
localSTlastAccessTime.wHour,localSTlastAccessTime.wMinute,localSTlastAccessTime.wSecond);
stLastWriteTime,&
localSTLastWriteTime);
最后写时间为:
localSTLastWriteTime.wYear,localSTLastWriteTime.wMonth,localSTLastWriteTime.wDay,localSTLastWriteTime.wHour,localSTLastWriteTime.wMinute,localSTLastWriteTime.wSecond);
}
4.获取文件属性:
DWORDdwAttr=GetFileAttributes("
if(dwAttr&
FILE_ATTRIBUTE_ARCHIVE)
归档\n"
FILE_ATTRIBUTE_COMPRESSED)
压缩\n"
FILE_ATTRIBUTE_ENCRYPTED)
加密\n"
FILE_ATTRIBUTE_HIDDEN)
隐藏\n"
FILE_ATTRIBUTE_NORMAL)
正常\n"
FILE_ATTRIBUTE_READONLY)
只读\n"
FILE_ATTRIBUTE_SYSTEM)
系统\n"
FILE_ATTRIBUTE_TEMPORARY)
临时\n"
6.获取文件详细信息