Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- void printRandomLine(char* filename);
- int main() {
- for(int i=1; i<5; i++) {
- int num = i;
- char snum[5];
- sprintf(snum, "%d", num);
- char str1[50], str2[50];
- strcpy(str1, "c");
- strcpy(str2, snum);
- strcat(str1, str2);
- strcat(str1, ".txt");
- printRandomLine(str1);
- }
- return 0;
- }
- void printRandomLine(char* filename) {
- FILE* s1 = fopen(filename, "r");
- char str[100];
- srandom(time(NULL));
- long rand = random() % 10;
- int lineCounter = 0;
- char c = fgetc(s1);
- while(lineCounter < rand) {
- if(c == '\n') {
- lineCounter++;
- }
- c = fgetc(s1);
- }
- fseek(s1, -1, SEEK_CUR);
- fgets(str, 100, s1);
- fputs(str, stdout);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement