home *** CD-ROM | disk | FTP | other *** search
/ Media Depot 5 / mediadepotvolume51993.iso / FILES / 13 / DEG.ZIP / REMOVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-13  |  1.7 KB  |  103 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <io.h>
  6. #include <bios.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9.  
  10. short nrecs;
  11. short reclen;
  12.  
  13. char in_file[16];
  14. char temp_file[16];
  15. char data_in[1000];
  16.  
  17. main(short argc, char *argv[])
  18. {
  19. FILE *fp;
  20. FILE *fpout;
  21. short cnt;
  22. short stat;
  23.  
  24.   if(argc != 2)
  25.   {
  26.     printf("Parameter Error or Filename Missing\n");
  27.     return 1;
  28.   }
  29.  
  30.   strcpy(in_file, argv[1]);
  31.   fp = fopen(in_file, "rb+");
  32.   if(fp == NULL)
  33.   {
  34.     printf("File Not Available - Press a key to continue\n");
  35.     getch();
  36.     fclose(fp);
  37.     return 1;
  38.   }
  39.   get_file_parms(fp);
  40.  
  41.   strcpy(temp_file, "ptemp.dat");
  42.   fpout = fopen(temp_file, "wb");
  43.   if(fpout == NULL)
  44.   {
  45.     printf("Trouble opening temporary file\n");
  46.     getch();
  47.     fclose(fp);
  48.     fclose(fpout);
  49.     return 1;
  50.   }
  51.  
  52.   do
  53.   {
  54.     stat = fread(data_in, reclen, 1, fp);
  55.  
  56.     cnt++;
  57.     if(data_in[0] != '!')
  58.     {
  59.       stat = fwrite(data_in, reclen, 1, fpout);
  60.     }
  61.     if(feof(fp))
  62.       break;
  63.   }
  64.   while(cnt < nrecs);
  65.  
  66.   fclose(fp);
  67.   if(fclose(fpout)==0)
  68.   {
  69.     remove(in_file);
  70.     rename(temp_file, in_file);
  71.     return 0;
  72.   }
  73.   return 1;
  74. }
  75.  
  76.  
  77. /* This routine figures no. of records based of length of first record */
  78. /* by how many characters to the Linefeed and then divides filesize by */
  79. /* record length obtaining number of records */
  80.  
  81. get_file_parms(fp)
  82. FILE *fp;
  83. {
  84. fpos_t position;
  85. char chr;
  86.  
  87.   nrecs = 0;
  88.   reclen = 0;
  89.   fseek(fp, 0L, SEEK_SET);
  90.   do
  91.   {
  92.     chr = fgetc(fp);
  93.     if(chr == EOF)
  94.       break;
  95.     reclen++;
  96.   }
  97.   while(chr != 10);
  98.  
  99.   nrecs = (short) (filelength(fileno(fp)) / reclen);
  100.   fseek(fp, 0L, SEEK_SET);
  101. }
  102.  
  103.