home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / filutl / mvline.arc / MV_LINE.C next >
Text File  |  1985-11-20  |  2KB  |  51 lines

  1. #include <stdio.h>
  2.  
  3. main(argc, argv)
  4. unsigned int argc;
  5. char **argv;
  6. {
  7. FILE *source, *dest, *temp;
  8. char line_buf[128];
  9. int f_flag = 0;
  10.  
  11.   if (argc != 4) {
  12.     printf("usage: mv_line <source_file> <dest_file> <key>.\n");
  13.     printf("This will move all lines beginning with <key> from\n");
  14.     printf("<source_file> to <dest_file>. Lines may have upto 127 chars.\n");
  15.     exit(1);
  16.   }
  17.   source = fopen(argv[1], "r");
  18.   dest   = fopen(argv[2], "a");
  19.   temp   = fopen("MV_LINE.$$$", "w");
  20.   if (source == NULL) {
  21.     printf("Sorry: cannot open source file: %s.\n", argv[1]);
  22.     exit(1);
  23.   }
  24.   if (dest == NULL) {
  25.     printf("Sorry: cannot open destination file: %s.\n", argv[2]);
  26.     exit(1);
  27.   }
  28.   if (temp == NULL) {
  29.     printf("Sorry: cannot create temporary file.\n");
  30.     exit(1);
  31.   }
  32.   while (! feof(source)) {
  33.     fgets(line_buf, 128, source);    /* get a line */
  34.     if (cmp_case(argv[3], line_buf) == 0) {
  35.       fputs(line_buf, dest);        /* move it to output. */
  36.       f_flag++;
  37.       continue;                /* don't copy to temp */
  38.     }
  39.     fputs(line_buf, temp);        /* copy no match line to temp */
  40.   }
  41.   if (! f_flag)
  42.     printf("Sorry: there are no lines in \"%s\" that match \"%s\".\n",
  43.       argv[1], argv[3]);
  44.   fclose(source);            /* close all i/o streams */
  45.   fclose(dest);
  46.   fclose(temp);
  47.   unlink(argv[1]);            /* destroy original */
  48.   rename("MV_LINE.$$$", argv[1]);    /* rename temp to original */
  49.   printf("Done. I moved %d line(s)\n", f_flag);
  50. }
  51.