24 windows_24_windows_file 文件系统 - 文件
Posted 养老保险年审
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了24 windows_24_windows_file 文件系统 - 文件相关的知识,希望对你有一定的参考价值。
windows_24_windows_file 文件系统 - 文件
// 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 convert
pFileItem // 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 文件系统 - 文件的主要内容,如果未能解决你的问题,请参考以下文章
使用Field_II_ver_3_24_windows_gcc工具箱实现超声波数据成像matlab仿真