Advertisement
xeritt

Delete from file max string with digit and rename outfile

Nov 29th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define N 1024
  7.  
  8. void renameTypeFile(char src[], char dest[], char *needle, char *text){
  9.     int c = strcspn(src, needle);
  10.     strncpy(dest, src, c);
  11.     strcat(dest, text);
  12. }
  13.  
  14. int check_str(uint8_t * str){
  15.     for (int i = 0; i < strlen((char *)str); i++) {
  16.         if (isdigit(str[i]) > 0) {
  17.             return 1;
  18.         }
  19.     }
  20.     return 0;
  21. }
  22.  
  23. int main(int argc, char **argv){
  24.     if (argc < 3) {
  25.         printf("Wrong count of params. Usage ./command [file_src_name] "
  26.                "[file_dest_name]\n");
  27.         exit(1);
  28.     }
  29.  
  30.     FILE *fp, *fp2;
  31.     if ((fp = fopen(argv[1], "r")) == NULL) {
  32.         printf("Error open file.\n");
  33.         exit(1);
  34.     }
  35.  
  36.     char str2[N];
  37.     renameTypeFile(argv[1], str2, ".", ".xyz");
  38.     if ((fp2 = fopen(str2, "w")) == NULL) {
  39.         printf("Error open file.\n");
  40.         exit(1);
  41.     }
  42.  
  43.     int max_str = atoi(argv[2]);
  44.  
  45.     uint8_t buffer[N];
  46.     int i = 0;
  47.     while (!feof(fp)) {
  48.         memset(buffer, '\0', N);
  49.         if (fgets((char *)buffer, N, fp)) {
  50.             if (i++ >= max_str)
  51.                 break;
  52.             if (0 == check_str(buffer)) {
  53.                 fputs((char *)buffer, fp2);
  54.                 printf("%s", buffer);
  55.             }
  56.         }
  57.     }
  58.     fclose(fp2);
  59.     fclose(fp);
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement