如何实时获得正在写入的文件的大小

www2t 2015-01-29 10:38:25

SECURITY_ATTRIBUTES saFile;
saFile.nLength = sizeof(SECURITY_ATTRIBUTES);
saFile.lpSecurityDescriptor = 0;
saFile.bInheritHandle = TRUE;

hOpenFile = CreateFile(m_strImagePath,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ,
&saFile,
CREATE_ALWAYS,
FILE_FLAG_WRITE_THROUGH,
NULL);

STARTUPINFO si={sizeof(si)};
si.hStdOutput = hOpenFile; //重定向到文件
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
PROCESS_INFORMATION pi;
ZeroMemory(&pi,sizeof(pi));

TCHAR* cmdline = L".....";//命令
CreateProcess(NULL,cmdline,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi);

.....do someting.....

调用CreateProcess把控制台输出重定向到文件,那么如何实时获得文件的大小
...全文
251 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-01-30
  • 打赏
  • 举报
回复
仅供参考
#pragma comment(lib,"user32")
#include <stdio.h>
#include <windows.h>
int main() {
    SECURITY_ATTRIBUTES sa          = {0};
    STARTUPINFO         si          = {0};
    PROCESS_INFORMATION pi          = {0};
    HANDLE              hPipeOutputRead  = NULL;
    HANDLE              hPipeOutputWrite = NULL;
    HANDLE              hPipeInputRead   = NULL;
    HANDLE              hPipeInputWrite  = NULL;
    BOOL                bTest = 0;
    DWORD               dwNumberOfBytesRead = 0;
    DWORD               dwNumberOfBytesWrite = 0;
    CHAR                szMsg[100];
    CHAR                szBuffer[256];

    sa.nLength = sizeof(sa);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = NULL;

    // Create pipe for standard output redirection.
    CreatePipe(&hPipeOutputRead,  // read handle
            &hPipeOutputWrite, // write handle
            &sa,      // security attributes
            0      // number of bytes reserved for pipe - 0 default
            );

    // Create pipe for standard input redirection.
    CreatePipe(&hPipeInputRead,  // read handle
            &hPipeInputWrite, // write handle
            &sa,      // security attributes
            0      // number of bytes reserved for pipe - 0 default
            );

    // Make child process use hPipeOutputWrite as standard out,
    // and make sure it does not show on screen.
    si.cb = sizeof(si);
    si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.wShowWindow = SW_HIDE;
    si.hStdInput   = hPipeInputRead;
    si.hStdOutput  = hPipeOutputWrite;
    si.hStdError   = hPipeOutputWrite;

    CreateProcess (
          NULL, "cmd.exe",
          NULL, NULL,
          TRUE, 0,
          NULL, NULL,
          &si, &pi);

    // Now that handles have been inherited, close it to be safe.
    // You don't want to read or write to them accidentally.
    CloseHandle(hPipeOutputWrite);
    CloseHandle(hPipeInputRead);

    // Now test to capture DOS application output by reading
    // hPipeOutputRead.  Could also write to DOS application
    // standard input by writing to hPipeInputWrite.
    sprintf(szMsg, "dir *.txt /b\nexit\n");
    WriteFile(
          hPipeInputWrite,      // handle of the write end of our pipe
          &szMsg,               // address of buffer that send data
          18,                   // number of bytes to write
          &dwNumberOfBytesWrite,// address of number of bytes read
          NULL                  // non-overlapped.
          );

    while(TRUE)
    {
       bTest=ReadFile(
          hPipeOutputRead,      // handle of the read end of our pipe
          &szBuffer,            // address of buffer that receives data
          256,                  // number of bytes to read
          &dwNumberOfBytesRead, // address of number of bytes read
          NULL                  // non-overlapped.
          );

      if (!bTest){
          sprintf(szMsg, "Error #%d reading pipe.",GetLastError());
          MessageBox(NULL, szMsg, "WinPipe", MB_OK);
          break;
      }

      // do something with data.
      szBuffer[dwNumberOfBytesRead] = 0;  // null terminate
      MessageBox(NULL, szBuffer, "WinPipe", MB_OK);
    }

    // Wait for CONSPAWN to finish.
    WaitForSingleObject (pi.hProcess, INFINITE);

    // Close all remaining handles
    CloseHandle (pi.hProcess);
    CloseHandle (hPipeOutputRead);
    CloseHandle (hPipeInputWrite);

    return 0;
}
www2t 2015-01-30
  • 打赏
  • 举报
回复
@luckylucky028 数据源是控制台输出,我是用CreateProcess重定向到文件的,所以不能用fwrite写文件
jiht594 2015-01-30
  • 打赏
  • 举报
回复
GetFileSize 取不到吗
赵4老师 2015-01-30
  • 打赏
  • 举报
回复
Fwatch Sample: Using ReadDirectoryChangesW API Click to open or copy the Fwatch project files. The Fwatch sample displays changes to watched directories and files in the command window. The directories and files to be watched are specified in the Fwatch.ini file, which you can leave in the application directory. Building SDK Samples This sample uses the following keywords: checkchangedfile; closehandle; createdirectory; createfile; createiocompletionport; createthread; exit; filetimetolocalfiletime; filetimetosystemtime; getch; getcurrentdirectory; getfileinformationbyhandle; getlasterror; getprivateprofilestring; getqueuedcompletionstatus; handledirectorychange; lstrcat; lstrcmpi; lstrcpy; lstrlen; postqueuedcompletionstatus; readdirectorychangesw; waitforsingleobject; watchdirectories; wprintf
东莞某某某 2015-01-30
  • 打赏
  • 举报
回复
开线程监控不行吗
luckylucky028 2015-01-29
  • 打赏
  • 举报
回复
seze_t count ; count = fwrite(const void* buffer, size_t size, size_t count, FILE* stream);

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧