zoukankan      html  css  js  c++  java
  • windows api 每日一练(3)文件操作

    参考:http://www.yesky.com/99/1824599.shtml

    1.OpenFile 全功能函数,可以打开,创建,删除文件

    HFILE WINAPI OpenFile(
      __in   LPCSTR lpFileName,
      __out  LPOFSTRUCT lpReOpenBuff,
      __in   UINT uStyle
    );


    2.获取文件长度

    DWORD WINAPI GetFileSize(
      __in       HANDLE hFile,
      __out_opt  LPDWORD lpFileSizeHigh
    );


    3.获取文件类型

    DWORD WINAPI GetFileType(
      __in  HANDLE hFile
    );


    4.获取指定文件时间信息

    BOOL WINAPI GetFileTime(
      __in       HANDLE hFile,
      __out_opt  LPFILETIME lpCreationTime,
      __out_opt  LPFILETIME lpLastAccessTime,
      __out_opt  LPFILETIME lpLastWriteTime
    );


    5.获取文件名

    (1)全名

    DWORD WINAPI GetFullPathName(
      __in   LPCTSTR lpFileName,
      __in   DWORD nBufferLength,
      __out  LPTSTR lpBuffer,
      __out  LPTSTR* lpFilePart
    );


    (2)长路径名

    DWORD WINAPI GetLongPathName(
      __in   LPCTSTR lpszShortPath,
      __out  LPTSTR lpszLongPath,
      __in   DWORD cchBuffer
    );

    (3)短路径名

    DWORD WINAPI GetShortPathName(
      __in   LPCTSTR lpszLongPath,
      __out  LPTSTR lpszShortPath,
      __in   DWORD cchBuffer
    );
    


    Test

    #include <windows.h>
    #include <stdio.h>
    #include <iostream>
    int main()
    {
      std::wstring originalStrFile(L"./test.txt");
    
    HANDLE hFile; 
    
    hFile = CreateFile(originalStrFile.c_str(),    // file to open
                       GENERIC_READ,          // open for reading
                       FILE_SHARE_READ,       // share for reading
                       NULL,                  // default security
                       OPEN_EXISTING,         // existing file only
                       FILE_ATTRIBUTE_NORMAL, // normal file
                       NULL);
    //获?取?文?件?大?小?
    DWORD dwSize =GetFileSize(hFile,NULL);
    //获?取?文?件?类?型?
    DWORD fileType=GetFileType(hFile);
    //获?取?文?件?时?间?信?息?
    FILETIME time1;
    FILETIME time2;
    FILETIME time3;
    GetFileTime(hFile,&time1,&time2,&time3);
    //获?取?文?件?名?
    DWORD bufferLength=100;
    TCHAR  buffer[100]=TEXT(""); 
    TCHAR* lpPart[100]={NULL};
    //获?取?全?名?
    GetFullPathName(originalStrFile.c_str(),bufferLength,buffer,lpPart);
    //获?取?长?路?径?名?
    GetLongPathName(originalStrFile.c_str(),buffer,bufferLength);
    //获?取?短?路?径?名?
    GetShortPathName(originalStrFile.c_str(),buffer,bufferLength);
    //关?闭?句?柄?对?象?
    CloseHandle(hFile);
    }
  • 相关阅读:
    IDEA tomcat热部署方法及乱码问题解决
    访问WEB-INF下JSP资源的几种方式(转)
    SpringMVC 静态资源处理
    SpringMVC中的拦截器
    SpringMVC中的异常处理
    SpringMVC实现文件上传
    IDEA 热部署
    响应数据和结果视图
    SpringMVC中的常用注解
    js获取当前根目录的方法
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1604391.html
Copyright © 2011-2022 走看看