home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / PROG / REXX / CMD / EDMI7.ZIP / BMPINFO.ZIP / FILE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-06  |  1.3 KB  |  61 lines

  1. #include <iostream.h>
  2. #include <io.h>
  3. #include <fcntl.h>
  4. #include <malloc.h>
  5.  
  6. int load(const char *szName, char* &bitmap) {
  7.   int fh=_open(szName,O_RDONLY | O_BINARY);
  8.   if (fh == -1) {
  9.     cout << "Error opening file " << szName << ".\n";
  10.     return 0;
  11.   }
  12.  
  13.   int length=_filelength(fh);
  14.   if (length == -1) {
  15.     cout << "Error determining length for " << szName << ".\n";
  16.     _close(fh);
  17.     return 0;
  18.   }
  19.   if (!length) {
  20.     cout << szName << " has zero filesize.\n";
  21.     _close(fh);
  22.     return 0;
  23.   }
  24.  
  25.   bitmap=(char *) malloc(length);
  26.   if (!bitmap) {
  27.     cout << "Error allocating " << length << " bytes.\n";
  28.     _close(fh);
  29.     return 0;
  30.   }
  31.  
  32.   int bytesread=_read(fh,bitmap,length);
  33.  
  34.   if (!bytesread) {
  35.     cout << "Read past end of file " << szName << ".\n";
  36.     free(bitmap);
  37.     _close(fh);
  38.     return 0;
  39.   }
  40.   if (bytesread == -1) {
  41.     cout << "Error reading file " << szName << ".\n";
  42.     free(bitmap);
  43.     _close(fh);
  44.     return 0;
  45.   }
  46.   if (bytesread != length) {
  47.     cout << "Could only read " << bytesread << " of " << length << " bytes.\n";
  48.     free(bitmap);
  49.     _close(fh);
  50.     return 0;
  51.   }
  52.  
  53.   if (_close(fh)) {
  54.     cout << "Error closing " << szName << ".\n";
  55.     free(bitmap);
  56.     return 0;
  57.   }
  58.  
  59.   return length;
  60. }
  61.