Advertisement
Guest User

Q12

a guest
Nov 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. void printRandomLine(char* filename);
  7.  
  8. int main() {
  9.  
  10.     for(int i=1; i<5; i++) {
  11.         int num = i;
  12.         char snum[5];
  13.         sprintf(snum, "%d", num);
  14.  
  15.         char str1[50], str2[50];
  16.         strcpy(str1, "c");
  17.         strcpy(str2, snum);
  18.         strcat(str1, str2);
  19.         strcat(str1, ".txt");
  20.         printRandomLine(str1);
  21.     }
  22.  
  23.     return 0;
  24. }
  25.  
  26. void printRandomLine(char* filename) {
  27.     FILE* s1 = fopen(filename, "r");
  28.     char str[100];
  29.  
  30.     srandom(time(NULL));
  31.     long rand = random() % 10;
  32.     int lineCounter = 0;
  33.     char c = fgetc(s1);
  34.  
  35.     while(lineCounter < rand) {
  36.         if(c == '\n') {
  37.             lineCounter++;
  38.         }
  39.         c = fgetc(s1);
  40.     }
  41.  
  42.     fseek(s1, -1, SEEK_CUR);
  43.  
  44.     fgets(str, 100, s1);
  45.     fputs(str, stdout);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement