Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define MAX_SIZE 50
- int main(void)
- {
- char fileName[] = "compliments.txt", chosenCompliment[MAX_SIZE] = {0};
- char test;
- FILE * compliments = fopen(fileName, "r");
- int i, lines = 0;
- if(compliments == NULL)
- {
- fprintf(stderr,"Could not open file, exiting\n");
- return 1;
- }
- srand(time(NULL));
- do
- {
- test = fgetc(compliments);
- if (test == '\n')
- lines++;
- }
- while(test != EOF);
- rewind(compliments);
- lines = rand() % lines;
- for(i=0;i<=lines;i++)
- fgets(chosenCompliment,100,compliments);
- printf("%s",chosenCompliment);
- fclose(compliments);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement