home *** CD-ROM | disk | FTP | other *** search
/ Amiga Computing 59 / ac059.adf / SupportFiles / files.c / files.c
C/C++ Source or Header  |  1993-02-05  |  3KB  |  95 lines

  1. #include <libraries/dos.h>
  2. #include <exec/memory.h>
  3.  
  4. long DosBase=0;                            /*Dos library base*/
  5. long consolefilehandle =0;                  /*console output filehandle*/
  6. long buffer=0;                              /*memory buffer pointer*/
  7. long inputfilehandle =0;                    /*input filehandle pointer*/
  8. long outputfilehandle =0;                   /*output filehandle pointer*/
  9. long bytes=0;                               /*bytes read*/
  10. char *filename="limerick";                  /*the file to be read*/
  11. char *filename2="limerick2";                /*the file to be written*/
  12. char *newfilecontents = "This is a new file \n"; /* data for the file*/
  13.  
  14.  
  15. /*proto types of the functions*/
  16. void cleanup();
  17. int readfile(), writefile();
  18.  
  19. main()
  20. {
  21.   DosBase= OpenLibrary(DOSNAME,0);      /*open the Dos library*/
  22.   if (DosBase == 0) 
  23.     {
  24.        cleanup ("Unable to open the Dos Library");
  25.        exit(1);                  /*exit if there is no library*/
  26.     }
  27.  
  28.   buffer=AllocMem(152,MEMF_PUBLIC|MEMF_CLEAR);    /*allocate memory buffer*/
  29.   if (buffer == 0)    
  30.     {
  31.        cleanup("No memory available"); 
  32.        exit(1);
  33.     };
  34.   bytes=readfile();                           /*read the source file*/
  35.   if (bytes ==  1)    
  36.     {
  37.       cleanup("Error opening and reading file\n"); 
  38.       exit(1);
  39.     };    
  40.   consolefilehandle=Output();                 /*obtain console file handle*/
  41.   Write(consolefilehandle,buffer,152);        /*write data to the screen*/
  42.   Delay(100);
  43.   bytes=writefile();                          /*create and write second file*/
  44.   if (bytes ==  1)    
  45.     {
  46.       cleanup("Error opening and writing file\n"); 
  47.       exit(1);
  48.      };    
  49.  
  50.   cleanup("files ok\n");                      /*close libraries, free buffer*/
  51.   return(0);
  52. }
  53.  
  54. void cleanup(errormsg)                        /* leave as you would wish */
  55. {                                      
  56.   printf(errormsg);                           /*display the meassage*/
  57.   if (buffer != 0 ) FreeMem(buffer,152);      /*free the memory*/
  58.   if (DosBase !=0) CloseLibrary(DosBase);     /*close library*/
  59.   if (inputfilehandle !=0) Close(inputfilehandle); /*close the files*/
  60.   if (outputfilehandle !=0) Close(outputfilehandle);
  61. }
  62.  
  63. int readfile()                                /*read the file*/
  64. {
  65.   inputfilehandle=Open(filename,MODE_OLDFILE); /*open the file, if possible*/
  66.   if (inputfilehandle == 0)          
  67.      {
  68.          printf("Cannot open file %s \n",filename);
  69.          return(1);
  70.       }
  71.   bytes=Read(inputfilehandle,buffer,152);   /*read the contents, if possible*/
  72.   if (bytes == 0) 
  73.      {
  74.         printf("Unable to read from %s \n",filename);
  75.         return(1);
  76.       }
  77.   return(0);
  78. }
  79.  
  80. int writefile()                             /*write a file*/
  81. {
  82.   int error=0;                                  
  83.   outputfilehandle=Open(filename2,MODE_NEWFILE); /*open a file if possible*/
  84.   if (outputfilehandle == 0) 
  85.      {
  86.        printf("Cannot open file %s \n",filename2);
  87.        return(1);
  88.      }
  89.   error=Write(outputfilehandle,newfilecontents,21); /*write message to file*/
  90.   error=Write(outputfilehandle,buffer,152);       /*append buffer contents*/
  91.   return(error);
  92. }
  93.  
  94.  
  95.