Advertisement
userxbw

Open file read into buffer

Aug 12th, 2022 (edited)
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. char *readIntoBuffer(FILE *fp);
  6.  
  7.  
  8. int random_int(int min, int max)
  9. {
  10.    return min + rand() % (max+1 - min);
  11. }
  12.  
  13.  
  14. FILE * openfile(char *file) {
  15. FILE *fptr;
  16. fptr = fopen(file,"r");
  17. if(fptr==NULL){
  18. printf("FILE B NULL\n");
  19. exit(1);
  20. }
  21. return fptr;
  22. }
  23. void printFile(char *f)
  24. {
  25. size_t sz=strlen(f);
  26.   for(int i=0;i<sz;i++){
  27.      printf("%c",f[i]);
  28.   }
  29. }
  30.  
  31.  
  32. void printRandomWords(char *w);
  33.  
  34. void mess1(){
  35. printf("Enter a filename "
  36. "to be opened\n");
  37. }
  38. void mess2(){
  39. printf("HEY, WHAT DID I TELL YOU?\n"
  40. "Now you have to start out "
  41. "all over again\n");
  42. }
  43.  
  44.  
  45. int main(int argc, char **argv){
  46. char *fp;
  47. mess1();
  48.  
  49. if(argc<2){
  50. mess2();
  51. exit(1);
  52. }
  53.  
  54. fp = strdup(argv[1]);
  55. FILE *file = openfile(fp);
  56. char *s=readIntoBuffer(file);
  57. printFile(s);
  58. printf("\n");
  59. printRandomWords(s);
  60. free(s);
  61. fclose(file);
  62. return 0;
  63. }
  64.  
  65. char *readIntoBuffer(FILE *fp){
  66. // unknown file size
  67. char *source = NULL;
  68.  
  69.     /* Go to the end of the file. */
  70.     if (fseek(fp, 0L, SEEK_END) == 0) {
  71.         /* Get the size of the file. */
  72.         long bufsize = ftell(fp);
  73.         if (bufsize == -1) { /* Error */ }
  74.  
  75.     /* Allocate our buffer to that size. */
  76.       source =
  77.       malloc(sizeof(char) * (bufsize + 1));
  78.  
  79.     /* Go back to the start of the file. */
  80.       if (fseek(fp, 0L, SEEK_SET) != 0)
  81.     { /* Error */ }
  82.  
  83.    /* Read the entire file into memory. */
  84.       size_t newLen =
  85.        fread(source, sizeof(char),
  86.        bufsize, fp);
  87.         if ( ferror( fp ) != 0 ) {
  88.             fputs("Error reading file",
  89.              stderr);
  90.         } else {
  91.             source[newLen++] = '\0';
  92.           /* Just to be safe add EOF */
  93.         }
  94.     }
  95. return source;
  96. }
  97.  
  98.  
  99. void printRandomWords(char *w){
  100. /*
  101. get x amount of random words
  102. from sorce store in an array
  103. for later use,return type should
  104. then be changed, or print
  105. ramdom words from source.
  106.  
  107. code goes here.
  108.  
  109.  
  110. It's a work in progress
  111. */
  112. size_t sz=strlen(w);
  113. //printf("%zu\n",sz);
  114. char *token;
  115. int c=0,ct,ct2=0;
  116.  
  117. ct=random_int(2,32);
  118. printf("ran %d\n\n",ct);
  119.   token=strtok(w, " ");
  120.  
  121.   while(token != NULL){
  122.       for(int i=0;i<ct;i++){
  123.           token=strtok(NULL, " ");
  124.        }
  125.     if(c==5){
  126.        printf("%s\n",token);
  127.        c=0;
  128.        ct2++;
  129.      }
  130.        if(ct2==5){
  131.      printf("\n");
  132.          ct2=0;
  133.       }
  134.   token=strtok(NULL, " ");
  135.   c++;
  136.  }
  137.  
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement