Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char** argv)
  6. {
  7.     int lenReda = 0;
  8.     char *novRed = 0, c;
  9.  
  10.     FILE* f = fopen("test.txt", "r");
  11.  
  12.     if (!f)
  13.     {
  14.         printf("Fajl nije pronadjen!");
  15.         getchar();
  16.         return 1;
  17.     }
  18.  
  19.     while (!feof(f))
  20.     {
  21.         lenReda = 0;
  22.  
  23.         // ucitamo iz fajla slovo po slovo dok ne dodjemo do karaktera za nov red
  24.         while ( (c = fgetc(f)) != '\n' && !feof(f))
  25.         {
  26.             novRed = realloc(novRed, (++lenReda + 1) * sizeof(char));
  27.             novRed[lenReda - 1] = c;
  28.         }
  29.  
  30.         // moramo da dodamo null terminator na kraj ucitanog stringa
  31.         novRed[lenReda] = '\0';
  32.  
  33.         printf("%s\n", novRed);
  34.  
  35.         // argv[0] je putanja do fajla
  36.         // prvi argument je argv[1]
  37.         if (strcmp(novRed, argv[1]) == 0)
  38.             printf("Linije se poklapaju.\n\n");
  39.         else
  40.             printf("Linije se ne poklapaju.\n\n");
  41.  
  42.         // obrisi iz memorije
  43.         free(novRed);
  44.         novRed = 0;
  45.     }
  46.  
  47.     // zatvorimo fajl
  48.     fclose(f);
  49.  
  50.     getchar();
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement