主题:新手, windows下的cat命令
代码如下
#include "windows.h"
#include "stdio.h"
#define BUF_SIZE 0x200
static VOID CatFile(HANDLE,HANDLE);
int main(int argc,LPTSTR argv [])
{
HANDLE hInFile,hStdIn=GetStdHandle(STD_INPUT_HANDLE);
HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
int i;
if(argc == 1)
{
CatFile(hStdIn,hStdOut);
return 0;
}
else
{
for(i = 1;i < argc ; i++)
{
hInFile = CreateFile(argv[i], GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(!(hInFile == INVALID_HANDLE_VALUE))
{
CatFile(hInFile,hStdOut);
}
CloseHandle(hInFile);
}
}
return 0;
}
static VOID CatFile(HANDLE hInFile,HANDLE hOutFile)
{
DWORD nIn,nOut;
BYTE Buffer [BUF_SIZE];
while(ReadFile(hInFile,Buffer,BUF_SIZE,&nIn,NULL) && (nIn != 0) && WriteFile(hOutFile,Buffer,nIn,&nOut,NULL));
return;
}
#include "windows.h"
#include "stdio.h"
#define BUF_SIZE 0x200
static VOID CatFile(HANDLE,HANDLE);
int main(int argc,LPTSTR argv [])
{
HANDLE hInFile,hStdIn=GetStdHandle(STD_INPUT_HANDLE);
HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
int i;
if(argc == 1)
{
CatFile(hStdIn,hStdOut);
return 0;
}
else
{
for(i = 1;i < argc ; i++)
{
hInFile = CreateFile(argv[i], GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(!(hInFile == INVALID_HANDLE_VALUE))
{
CatFile(hInFile,hStdOut);
}
CloseHandle(hInFile);
}
}
return 0;
}
static VOID CatFile(HANDLE hInFile,HANDLE hOutFile)
{
DWORD nIn,nOut;
BYTE Buffer [BUF_SIZE];
while(ReadFile(hInFile,Buffer,BUF_SIZE,&nIn,NULL) && (nIn != 0) && WriteFile(hOutFile,Buffer,nIn,&nOut,NULL));
return;
}