Guest User

Untitled

a guest
Nov 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. char* get_word(FILE* input);
  7. void input_free (char **buffer, int size);
  8.  
  9. int main(int argc, char *argv[]) {
  10.    FILE *input, *output;
  11.    char *temp;
  12.    char **buffer = NULL;
  13.    int i;
  14.    int size = 0;
  15.    int status;
  16.    
  17.    if (argc != 3) {
  18.       printf ("Invalid number of parameters\n");
  19.       exit (1);
  20.    }
  21.  
  22. /* Odczyt */
  23.  
  24.    input = fopen(argv[1], "r");
  25.    if(input == NULL) {
  26.       printf("Error. Cannot open input file.\n");
  27.       exit (1);
  28.    }
  29.  
  30.    while (1) {
  31.       temp = get_word (input);
  32.       if (temp == NULL) break;
  33.       if (buffer == NULL) {
  34.          size++;
  35.          buffer = (char **)malloc (sizeof(char *));
  36.       }
  37.       else {
  38.          size++;
  39.          buffer = (char **)realloc (buffer, size * sizeof(char *));
  40.       }
  41.       buffer[size-1] = temp;
  42.    }
  43.    fclose (input);
  44.    
  45.    printf("\n");
  46.    for (i=0; i<size; i++) {
  47.       printf("%s",buffer[i]);
  48.    }
  49.    printf("\n");
  50.  
  51. /* Zapis */
  52.  
  53.    output = fopen (argv[2], "w");
  54.    if (output == NULL) {
  55.       printf ("Cannot open output file\n");
  56.       input_free (buffer, size);
  57.       exit (1);
  58.    }
  59.  
  60.    for (i=0; i<size; i++) {
  61.       status = fputs (buffer[i], output);
  62.       if (status == EOF) {
  63.          printf ("Error while writing to a output file\n");
  64.          input_free (buffer, size);
  65.          exit (1);
  66.       }
  67.    }
  68.    fclose (output);
  69.  
  70. /* Dealokacja */
  71.  
  72.    input_free (buffer, size);
  73.    
  74.    return 0;
  75. }
  76.  
  77. char *get_word (FILE* input) {
  78.    char *word = NULL;
  79.    int temp;
  80.    int size = 0;
  81.    
  82.    while (1) {
  83.       temp = fgetc (input);
  84.       if (temp == EOF) {
  85.          if (word == NULL) return NULL;
  86.          else break;
  87.       }
  88.       size++;
  89.       if (word == NULL) {
  90.          word = malloc (sizeof(char));
  91.          word[0] = (char)temp;
  92.       }
  93.       else {
  94.          word = (char *)realloc ((void *)word, (size_t) size);
  95.          word[size-1] = (char)temp;
  96.       }
  97.       if ((temp == '\n')||(temp == ' ')||(temp == '\t')) break;
  98.    }
  99.    word = (char *)realloc ((void *)word, (size_t) (size+1));
  100.    word[size] = '\0';
  101.    
  102.    return word;
  103. }
  104.  
  105. void input_free (char **buffer, int size) {
  106.    int i;
  107.    
  108.    for (i=0; i<size; i++) {
  109.       free (buffer[i]);
  110.       buffer[i] = NULL;
  111.    }
  112.    free (buffer);
  113.    buffer = NULL;
  114. }
Add Comment
Please, Sign In to add comment