Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2618198
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: WINDOWS

2013-10-06 18:35:25


点击(此处)折叠或打开

  1. /*
  2.  * File: main.cpp
  3.  * Author: Administrator
  4.  *
  5.  * Created on 2013年10月6日, 下午3:14
  6.  */

  7. #include <cstdlib>
  8. #include <windows.h>
  9. #include <iostream>

  10. using namespace std;

  11. ///< 暂时不管 -> ReadFileEx
  12. VOID CALLBACK FileIOCompletionRoutine(
  13.         DWORD dwErrorCode,
  14.         DWORD dwNumberOfBytesTransfered,
  15.         LPOVERLAPPED lpOverlapped
  16. )
  17. {
  18.     
  19. }

  20. DWORD ReadSomething(LPCTSTR lpFileName, LPVOID lpBuffer, DWORD nNumberOfBytesToRead)
  21. {
  22.     BOOL rc;
  23.     HANDLE file; ///< typedef void * HANDLE;
  24.     DWORD numread;
  25.     OVERLAPPED overlap;
  26.     
  27.     ///< openg file 具体参数见 http://msdn.microsoft.com
  28.     file = CreateFile(lpFileName,
  29.             GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  30.             NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
  31.     if (INVALID_HANDLE_VALUE == file)
  32.     {
  33.         return -1;
  34.     }
  35.     
  36.     ///< initial overlapped
  37.     memset(&overlap, 0, sizeof(overlap));
  38.     overlap.Offset = 0;
  39.     
  40.     ///< read data
  41.     rc = ReadFile(file, (char *)lpBuffer, nNumberOfBytesToRead, &numread, &overlap);
  42.     if (true == rc) ///< success
  43.     {
  44.         return numread;
  45.     }
  46.     else
  47.     {
  48.         ///< 是否在排队
  49.         if (GetLastError() == ERROR_IO_PENDING)
  50.         {
  51.             ///< awhile...
  52.             //WaitForSingleObject(file, INFINITE);
  53.             if ((rc = GetOverlappedResult(file, &overlap, &numread, FALSE)))
  54.             {
  55.                  return numread;
  56.             }
  57.             else
  58.             {
  59.                 if (GetLastError() == ERROR_IO_INCOMPLETE)
  60.                 {
  61.                     ///< not ok.
  62.                     return -1;
  63.                 }
  64.             }
  65.         }
  66.         else
  67.         {
  68.             ///< something wrong.
  69.             return -2;
  70.         }
  71.     }
  72.     CloseHandle(file);
  73.     
  74.     return 0;
  75. }

  76. /*
  77.  *
  78.  */
  79. int main(int argc, char** argv)
  80. {
  81.     //cout << "just yeah!" << endl;
  82.     //Beep(1000,500);
  83.     //Sleep(1000);
  84.     char buf[512] = {0};
  85.     DWORD numread = ReadSomething("E:\\NetBeansProjects\\CppApplication_1\\dist\\Debug\\MinGW-Windows\\ddd.txt",
  86.             buf, 10);
  87.     cout << "counts: " << numread << " content: " << buf << endl;
  88.     //MessageBox(NULL,"示例用法","ddd", MB_OK);
  89.     return 0;
  90. }
阅读(3935) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~