Advertisement
nervouslovesnikki

open file read into buffer

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