home *** CD-ROM | disk | FTP | other *** search
- #include <libraries/dos.h>
- #include <exec/memory.h>
-
- long DosBase=0; /*Dos library base*/
- long consolefilehandle =0; /*console output filehandle*/
- long buffer=0; /*memory buffer pointer*/
- long inputfilehandle =0; /*input filehandle pointer*/
- long outputfilehandle =0; /*output filehandle pointer*/
- long bytes=0; /*bytes read*/
- char *filename="limerick"; /*the file to be read*/
- char *filename2="limerick2"; /*the file to be written*/
- char *newfilecontents = "This is a new file \n"; /* data for the file*/
-
-
- /*proto types of the functions*/
- void cleanup();
- int readfile(), writefile();
-
- main()
- {
- DosBase= OpenLibrary(DOSNAME,0); /*open the Dos library*/
- if (DosBase == 0)
- {
- cleanup ("Unable to open the Dos Library");
- exit(1); /*exit if there is no library*/
- }
-
- buffer=AllocMem(152,MEMF_PUBLIC|MEMF_CLEAR); /*allocate memory buffer*/
- if (buffer == 0)
- {
- cleanup("No memory available");
- exit(1);
- };
- bytes=readfile(); /*read the source file*/
- if (bytes == 1)
- {
- cleanup("Error opening and reading file\n");
- exit(1);
- };
- consolefilehandle=Output(); /*obtain console file handle*/
- Write(consolefilehandle,buffer,152); /*write data to the screen*/
- Delay(100);
- bytes=writefile(); /*create and write second file*/
- if (bytes == 1)
- {
- cleanup("Error opening and writing file\n");
- exit(1);
- };
-
- cleanup("files ok\n"); /*close libraries, free buffer*/
- return(0);
- }
-
- void cleanup(errormsg) /* leave as you would wish */
- {
- printf(errormsg); /*display the meassage*/
- if (buffer != 0 ) FreeMem(buffer,152); /*free the memory*/
- if (DosBase !=0) CloseLibrary(DosBase); /*close library*/
- if (inputfilehandle !=0) Close(inputfilehandle); /*close the files*/
- if (outputfilehandle !=0) Close(outputfilehandle);
- }
-
- int readfile() /*read the file*/
- {
- inputfilehandle=Open(filename,MODE_OLDFILE); /*open the file, if possible*/
- if (inputfilehandle == 0)
- {
- printf("Cannot open file %s \n",filename);
- return(1);
- }
- bytes=Read(inputfilehandle,buffer,152); /*read the contents, if possible*/
- if (bytes == 0)
- {
- printf("Unable to read from %s \n",filename);
- return(1);
- }
- return(0);
- }
-
- int writefile() /*write a file*/
- {
- int error=0;
- outputfilehandle=Open(filename2,MODE_NEWFILE); /*open a file if possible*/
- if (outputfilehandle == 0)
- {
- printf("Cannot open file %s \n",filename2);
- return(1);
- }
- error=Write(outputfilehandle,newfilecontents,21); /*write message to file*/
- error=Write(outputfilehandle,buffer,152); /*append buffer contents*/
- return(error);
- }
-
-
-