#include "Everything.h" // this is book's own defined header
// it includes windows.h etc
#define BUF_SIZE 0x200
static VOID CatFile (HANDLE, HANDLE); // method
int _tmain (int argc, LPTSTR argv [])
{
HANDLE hInFile, hStdIn = GetStdHandle (STD_INPUT_HANDLE); // make an input handler, see the return type
HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE); // the handle where we want to write
BOOL dashS;
int iArg, iFirstFile;
/* dashS will be set only if "-s" is on the command line. */
/* iFirstFile is the argv [] index of the first input file. */
iFirstFile = Options (argc, argv, _T("s"), &dashS, NULL);
if (iFirstFile == argc) { /* No files in arg list. */
CatFile (hStdIn, hStdOut);
return 0;
}
/* Process the input files. */
for (iArg = iFirstFile; iArg < argc; iArg++) {
hInFile = CreateFile (argv [iArg], GENERIC_READ,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); // Create file with
if (hInFile == INVALID_HANDLE_VALUE) { // if the file is not appropriate
if (!dashS) ReportError (_T ("Cat Error: File does not exist."), 0, TRUE);
} else {
CatFile (hInFile, hStdOut);
if (GetLastError() != 0 && !dashS) {
ReportError (_T ("Cat Error: Could not process file completely."), 0, TRUE);
}
CloseHandle (hInFile);
}
}
return 0;
}
static VOID CatFile (HANDLE hInFile, HANDLE hOutFile)
{
DWORD nIn, nOut; // Dword mean easily 32bit unsigned int
BYTE buffer [BUF_SIZE];
while (ReadFile (hInFile, buffer, BUF_SIZE, &nIn, NULL) && (nIn != 0)
&& WriteFile (hOutFile, buffer, nIn, &nOut, NULL));
// Read the file and write exactly.
return;
}
Easy example, take a look at the CatFile, using windows API, ReadFile and WriteFile, makes a copy of the
original file, argv[1]
LPTSTR -> Long Pointer something something, in other words, like char*
// it includes windows.h etc
#define BUF_SIZE 0x200
static VOID CatFile (HANDLE, HANDLE); // method
int _tmain (int argc, LPTSTR argv [])
{
HANDLE hInFile, hStdIn = GetStdHandle (STD_INPUT_HANDLE); // make an input handler, see the return type
HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE); // the handle where we want to write
BOOL dashS;
int iArg, iFirstFile;
/* dashS will be set only if "-s" is on the command line. */
/* iFirstFile is the argv [] index of the first input file. */
iFirstFile = Options (argc, argv, _T("s"), &dashS, NULL);
if (iFirstFile == argc) { /* No files in arg list. */
CatFile (hStdIn, hStdOut);
return 0;
}
/* Process the input files. */
for (iArg = iFirstFile; iArg < argc; iArg++) {
hInFile = CreateFile (argv [iArg], GENERIC_READ,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); // Create file with
if (hInFile == INVALID_HANDLE_VALUE) { // if the file is not appropriate
if (!dashS) ReportError (_T ("Cat Error: File does not exist."), 0, TRUE);
} else {
CatFile (hInFile, hStdOut);
if (GetLastError() != 0 && !dashS) {
ReportError (_T ("Cat Error: Could not process file completely."), 0, TRUE);
}
CloseHandle (hInFile);
}
}
return 0;
}
static VOID CatFile (HANDLE hInFile, HANDLE hOutFile)
{
DWORD nIn, nOut; // Dword mean easily 32bit unsigned int
BYTE buffer [BUF_SIZE];
while (ReadFile (hInFile, buffer, BUF_SIZE, &nIn, NULL) && (nIn != 0)
&& WriteFile (hOutFile, buffer, nIn, &nOut, NULL));
// Read the file and write exactly.
return;
}
Easy example, take a look at the CatFile, using windows API, ReadFile and WriteFile, makes a copy of the
original file, argv[1]
LPTSTR -> Long Pointer something something, in other words, like char*
'Programming' 카테고리의 다른 글
Spring3.0 ajax를 이용한 json 주고밖기(@RequestBody) (0) | 2012.11.18 |
---|---|
Open SSH Server (0) | 2012.10.08 |
Being fillled circle / Android programming / Thread example (0) | 2011.10.15 |
Win32 API tutorial (0) | 2011.08.26 |