home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / File Access Examples 1.0 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  2.1 KB  |  91 lines  |  [TEXT/KAHL]

  1. /* main.c
  2.  *
  3.  * A short example program which uses some of the functions in
  4.  * FileRoutines.c and the scanning routine in Scanner.c. In this
  5.  * example, I ignore the Boolean return values of the file access
  6.  * routines. You shouldn't do this in your programs.
  7.  */
  8.  
  9. #include "FileRoutines.h"
  10.  
  11. #include "Scanner.h"
  12.  
  13. short myFileRef;    /* global file reference number */
  14.  
  15. void main( void )
  16. {
  17.     StandardFileReply    myReply;
  18.     OSErr                myErr;
  19.     char                x;
  20.  
  21.     /* Initialize the Macintosh toolbox */
  22.     
  23.         InitGraf( &qd.thePort );
  24.         InitFonts();
  25.         InitWindows();
  26.         InitMenus();
  27.         TEInit();
  28.         InitDialogs( 0L );
  29.         InitCursor();
  30.         FlushEvents( everyEvent, 0 );
  31.  
  32.     /* Ask user for file spec to create new file */
  33.     
  34.         StandardPutFile( "\pSave as:", "\p", &myReply );
  35.     
  36.     /* If cancelled, exit here */
  37.     
  38.         if ( !myReply.sfGood )
  39.             ExitToShell();
  40.             
  41.     /* If we're replacing an existing file, delete the old one */
  42.     
  43.         if ( myReply.sfReplacing )
  44.             myErr = FSpDelete( &myReply.sfFile );
  45.  
  46.     /* Now create a new file (a TeachText file) */
  47.     
  48.         myErr = FSpCreate( &myReply.sfFile, 'ttxt', 'TEXT',
  49.                                             myReply.sfScript );
  50.  
  51.     /* Now open the file for reading and writing */
  52.     /* storing the reference number in global myFileRef */
  53.     
  54.         myErr = FSpOpenDF( &myReply.sfFile,
  55.                                     fsRdWrPerm, &myFileRef );
  56.  
  57.     /* Write the characters 'A' through 'Z' to the file */
  58.     /* using the WriteByte routine sequentially */
  59.     
  60.         for ( x=65; x<=90; x++ )
  61.             WriteByte( myFileRef, kSequential, x );
  62.     
  63.     /* Now, use direct access method to replace some of */
  64.     /* those letters with something that suits me better. */
  65.     
  66.         WriteByte( myFileRef, 11, 'e' );
  67.         WriteByte( myFileRef, 12, 'n' );
  68.         WriteByte( myFileRef, 13, ' ' );
  69.     
  70.     /* Put the file mark back at the end of the file */
  71.     
  72.         SetCurrentRecord( myFileRef,
  73.                     RecordsInFile( myFileRef, 1 ), 1 );
  74.         
  75.     /* Write a carriage return to the file */
  76.     
  77.         WriteByte( myFileRef, kSequential, '\r' );
  78.     
  79.     /* Call ScanVolumes to scan all mounted volumes and */
  80.     /* record all folder names in the file */
  81.     
  82.         ScanVolumes();
  83.     
  84.     /* Now close the file */
  85.     
  86.         FSClose( myFileRef );
  87.     
  88.     /* And we're done! */
  89.     
  90.         ExitToShell();
  91. }