home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / newemacs / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  1.9 KB  |  105 lines

  1. /*
  2.  * The routines in this file
  3.  * read and write ASCII files from the
  4.  * disk. All of the knowledge about files
  5.  * are here. A better message writing
  6.  * scheme should be used.
  7.  */
  8. #define         EOFCHAR 0x1A
  9. #include    <stdio.h>
  10. #include    "ed.h"
  11.  
  12. FILE    *ffp;                /* File pointer, all functions.    */
  13.  
  14. /*
  15.  * Open a file for reading.
  16.  */
  17. ffropen(fn)
  18. char    *fn;
  19. {
  20.     if ((ffp=fopen(fn, "r")) == NULL)
  21.         return (FIOFNF);
  22.     return (FIOSUC);
  23. }
  24.  
  25. /*
  26.  * Open a file for writing.
  27.  * Return TRUE if all is well, and
  28.  * FALSE on error (cannot create).
  29.  */
  30. ffwopen(fn)
  31. char    *fn;
  32. {
  33.     if ((ffp=fopen(fn, "w")) == NULL) {
  34.         mlwrite("Cannot open file for writing");
  35.         return (FIOERR);
  36.     }
  37.     return (FIOSUC);
  38. }
  39.  
  40. /* write an eof mark at end of file */
  41. weof()
  42.     {
  43.     if (fputc(EOFCHAR,ffp))
  44.         return(FIOSUC);
  45.     else
  46.         return(FIOERR);
  47.     }
  48.  
  49. /*
  50.  * Close a file.
  51.  * Should look at the status in all systems.
  52.  */
  53. ffclose()
  54. {
  55.     fclose(ffp);
  56.     return (FIOSUC);
  57. }
  58.  
  59. /*
  60.  * Write a line to the already
  61.  * opened file. The "buf" points to the
  62.  * buffer, and the "nbuf" is its length, less
  63.  * the free newline. Return the status.
  64.  * Check only at the newline.
  65.  */
  66. ffputline(buf, nbuf)
  67. register char    buf[];
  68. {
  69.     register int    i;
  70.     static char    crlf[] = "\r\n" ;
  71.  
  72.     if (nbuf==0 || fwrite(buf, 1, nbuf, ffp))
  73.         {
  74.         if (fwrite(crlf, 1, 2, ffp))
  75.             {
  76.             return FIOSUC ;
  77.             }
  78.         }
  79.     mlwrite("Write I/O error");
  80.     return (FIOERR);
  81. }
  82.  
  83. /*
  84.  * Read a line from a file,
  85.  * and store the bytes in the supplied
  86.  * buffer. The "nbuf" is the length of the
  87.  * buffer. Complain about long lines and lines
  88.  * at the end of the file that don't have a
  89.  * newline present. Check for I/O errors
  90.  * too. Return status.
  91.  */
  92. ffgetline(buf, nbuf)
  93. register char    buf[];
  94. {
  95.     register int    c;
  96.     register int    i;
  97.  
  98.     if (fgets(buf, nbuf, ffp))
  99.         {
  100.         buf[strlen(buf) - 1] = 0 ;
  101.         return  (FIOSUC) ;
  102.         }
  103.     return FIOEOF ;
  104. }
  105.