/////////////////////////////////////////////////////////////// // Win32Files.cpp - demonstrate reading and writing with // // Win32 File API // // ver 1.0 // // // // Jim Fawcett, CSE 691/791 - SWM&A, Fall 2001 // /////////////////////////////////////////////////////////////// #include #include #include #include "sysUtils.h" using namespace std; int main(int argc, char *argv[]) { cout << "\n Demonstrating Win32 File API " << "\n ==============================\n\n"; if(argc == 1) { cout << "\n please enter name of file to open on command line\n\n"; return 1; } char *fileName = argv[1]; // attempt to open file named in first argument of the command line HANDLE fileHandle = CreateFile( fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL ); sysError err; if(fileHandle == INVALID_HANDLE_VALUE) { cout << "\n " << err.getLastMsg() << endl; return 1; } // read file in 20 byte blocks and write to stdout unsigned long int numRead, numWritten; HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE); const int bufferSize = 20; char buffer[bufferSize]; BOOL readOK = TRUE; while(1) { readOK = ReadFile(fileHandle,buffer,bufferSize,&numRead,NULL); if(readOK && numRead > 0) { WriteFile(StdOut,buffer,numRead,&numWritten,NULL); } else break; } CloseHandle(fileHandle); cout << "\n\n"; return 0; }