home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / desklib / Examples / File next >
Encoding:
Text File  |  1993-07-12  |  1.9 KB  |  59 lines

  1. This is an excerpt of C code that can be used to test the File_ library
  2. code. It can be pasted into TestApp just after the hourglass test loop.
  3. It also serves as a very basic example of usage of File_ calls.
  4.  
  5.   /* Test the file code by writing some stuff out, reading it back in */
  6.   {
  7.     file_handle infile, outfile;
  8.     char *teststring = "This is a test string";
  9.     char buffer[32];
  10.  
  11.     outfile = File_Open("<Tester$Dir>.testfile", file_WRITE);
  12.     if (outfile == NULL)
  13.     {
  14.       if (file_lasterror != NULL)
  15.         Error_Report(1, "Can't open testfile for writing: %s",
  16.                         file_lasterror->errmess);
  17.     }                    
  18.     else
  19.     {
  20.       Error_Check(File_WriteBytes(outfile, teststring, 21));
  21.       Error_Check(File_Write8(outfile, '*'));
  22.       Error_Check(File_Write32(outfile, 'Zot!'));
  23.       Error_Check(File_Write32R(outfile, 'Zot!'));
  24.       Error_Check(File_Close(outfile));
  25.     }
  26.  
  27.     if (File_Exists("<Tester$Dir>.testfile"))
  28.     {
  29.       infile = File_Open("<Tester$Dir>.testfile", file_READ);
  30.       if (infile == NULL)
  31.       {
  32.         if (file_lasterror != NULL)
  33.           Error_Report(1, "Can't open testfile for reading: %s",
  34.                           file_lasterror->errmess);
  35.       }
  36.       else
  37.       {
  38.         if (File_Size("<Tester$Dir>.testfile") != 30)
  39.           Error_Report(1, "Test file is not the length we expect! \n");
  40.  
  41.         File_ReadBytes(infile, buffer, 21);
  42.         if (strncmp(buffer, teststring, 21))
  43.           Error_Report(1, "File_Write/ReadBytes fails test! \n");
  44.  
  45.         if (File_Read8(infile) != '*')
  46.           Error_Report(1, "File_Write/Read8 fails test! \n");
  47.  
  48.         if (File_Read32(infile) != 'Zot!')
  49.           Error_Report(1, "File_Write/Read32 fails test! \n");
  50.  
  51.         if (File_Read32R(infile) != 'Zot!')
  52.           Error_Report(1, "File_Write/Read32R fails test! \n");
  53.  
  54.         File_Close(infile);
  55.       }
  56.     }
  57.   }
  58.   Error_Check(File_Delete("<Tester$Dir>.testfile"));
  59.