// windows_24_windows_file.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <windows.h>void Create( ){//创建文件HANDLE hFile = CreateFile( "D:\\MyTest.txt",GENERIC_READ | GENERIC_WRITE,0/*独占模式*/,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL );//关闭文件CloseHandle( hFile );}//写数据void Write( ){//打开文件HANDLE hFile = CreateFile( "D:\\MyTest.txt",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );if (hFile == NULL){printf( "error:handle\n" );return;}//写入数据CHAR szBuff[] = "Hello File";DWORD nWritten = 0;WriteFile( hFile, szBuff, strlen( szBuff ), &nWritten, NULL );//关闭文件CloseHandle( hFile );}void Read( ){//打开文件HANDLE hFile = CreateFile( "D:\\MyTest.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );//获取文件长度DWORD nHigh = 0;DWORD nLow = GetFileSize( hFile, &nHigh );//分配空间LPSTR pszBuf = (LPSTR)malloc( nLow + 1 );memset( pszBuf, 0, nLow + 1 );//设置当前读取的位置SetFilePointer( hFile, 1, NULL, FILE_BEGIN );//读取数据DWORD nRead = 0;ReadFile( hFile, pszBuf, nLow + 1, &nRead, NULL );printf( "%s", pszBuf );free( pszBuf );//关闭文件CloseHandle( hFile );}//操作void Operate( ){//复制文件CopyFile( "D:\\MyTest.txt", "D:\\MyTest1.txt", FALSE );//删除文件DeleteFile( "D:\\MyTest.txt" );}//打印时间void PrintFileTime( LPSTR pszName, LPFILETIME pFileItem ){//转换成本时区时间FileTimeToLocalFileTime(pFileItem, // pointer to UTC file time to convertpFileItem // pointer to converted file time);//将文件时间转换成系统时间SYSTEMTIME systime = { 0 };FileTimeToSystemTime( pFileItem, &systime );printf( "\n%s %d-%d-%d %d:%d:%d", pszName, systime.wYear, systime.wMonth,systime.wDay, systime.wHour, systime.wMinute, systime.wSecond );}//获取属性void Attri( ){//获取文件的基本属性DWORD nAttri = GetFileAttributes( "D:\\MyTest1.txt" );printf( "nAttri = %0X\n", nAttri );//获取文件的属性信息WIN32_FILE_ATTRIBUTE_DATA data = { 0 };GetFileAttributesEx( "D:\\MyTest1.txt", GetFileExInfoStandard, &data );printf( "\nAttri=%d", data.dwFileAttributes );printf( "\nSizeHigh=%d", data.nFileSizeHigh );printf( "\nSizeLow=%d",data.nFileSizeLow );PrintFileTime( "\nCreateTime:", &data.ftCreationTime );PrintFileTime( "\nCreateTime:", &data.ftLastAccessTime );PrintFileTime( "\nCreateTime:", &data.ftLastWriteTime );}int _tmain(int argc, _TCHAR* argv[]){Create( );Write( );Read( );Operate( );Attri( );return 0;}
24 windows_24_windows_file 文件系统 - 文件
原文:http://www.cnblogs.com/nfking/p/5573352.html