将程序的标准输出导入到了一个文件,怎么限制文件的大小?
我使用createFile函数创建了一个文件,然后将它的句柄赋给createProcess中STARTUPINFO中的hStdOutput,这样我就可以把调用cmd的输出写入到那个文件里
但是我希望限制这个文件的大小,比如最多1k字节,怎么实现比较好呢?
谢了
以下是我的测试函数
#include <windows.h>
#include <stdio.h>
void main( VOID )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
HANDLE file;
SECURITY_ATTRIBUTES sa;
ZeroMemory( &sa, sizeof(sa) );
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = true;
file = CreateFile( "aaa.txt ", GENERIC_WRITE, FILE_SHARE_WRITE, &sa, CREATE_ALWAYS, 0, NULL);
if (file == INVALID_HANDLE_VALUE) {
printf( "fdsafdsafsa\n ");
return;
}
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
si.hStdOutput = file;
si.dwFlags = STARTF_USESTDHANDLES;
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
"hostname ", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
true, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent 's environment block.
NULL, // Use parent 's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed (%d).\n ", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle(file);
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
[解决办法]
GetFileSize() API
就知道文件的大小了
[解决办法]
创建中间层。不要直接写文件嘛。
dos的重定向是> 和> > ,那还有管理命令“|”可用嘛