Advertisement
plaguenurse

cygnet-basic.c

Oct 23rd, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #define MAX_SIZE 50
  6. int main(void)
  7. {
  8.     char fileName[] = "compliments.txt", chosenCompliment[MAX_SIZE] = {0};
  9.     char test;
  10.     FILE * compliments = fopen(fileName, "r");
  11.     int i, lines = 0;
  12.    
  13.     if(compliments == NULL)
  14.     {
  15.         fprintf(stderr,"Could not open file, exiting\n");
  16.         return 1;
  17.     }
  18.     srand(time(NULL));
  19.     do
  20.     {
  21.         test = fgetc(compliments);
  22.         if (test == '\n')
  23.             lines++;
  24.     }
  25.     while(test != EOF);
  26.     rewind(compliments);
  27.     lines = rand() % lines;
  28.     for(i=0;i<=lines;i++)
  29.         fgets(chosenCompliment,100,compliments);
  30.     printf("%s",chosenCompliment);
  31.     fclose(compliments);
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement