Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- char *readIntoBuffer(FILE *fp);
- int random_int(int min, int max)
- {
- return min + rand() % (max+1 - min);
- }
- FILE * openfile(char *file) {
- FILE *fptr;
- fptr = fopen(file,"r");
- if(fptr==NULL){
- printf("FILE B NULL\n");
- exit(1);
- }
- return fptr;
- }
- void printFile(char *f)
- {
- size_t sz=strlen(f);
- for(int i=0;i<sz;i++){
- printf("%c",f[i]);
- }
- }
- void printRandomWords(char *w);
- void mess1(){
- printf("Enter a filename "
- "to be opened\n");
- }
- void mess2(){
- printf("HEY, WHAT DID I TELL YOU?\n"
- "Now you have to start out "
- "all over again\n");
- }
- int main(int argc, char **argv){
- char *fp;
- mess1();
- if(argc<2){
- mess2();
- exit(1);
- }
- fp = strdup(argv[1]);
- FILE *file = openfile(fp);
- char *s=readIntoBuffer(file);
- printFile(s);
- printf("\n");
- printRandomWords(s);
- free(s);
- fclose(file);
- return 0;
- }
- char *readIntoBuffer(FILE *fp){
- // unknown file size
- char *source = NULL;
- /* Go to the end of the file. */
- if (fseek(fp, 0L, SEEK_END) == 0) {
- /* Get the size of the file. */
- long bufsize = ftell(fp);
- if (bufsize == -1) { /* Error */ }
- /* Allocate our buffer to that size. */
- source =
- malloc(sizeof(char) * (bufsize + 1));
- /* Go back to the start of the file. */
- if (fseek(fp, 0L, SEEK_SET) != 0)
- { /* Error */ }
- /* Read the entire file into memory. */
- size_t newLen =
- fread(source, sizeof(char),
- bufsize, fp);
- if ( ferror( fp ) != 0 ) {
- fputs("Error reading file",
- stderr);
- } else {
- source[newLen++] = '\0';
- /* Just to be safe add EOF */
- }
- }
- return source;
- }
- void printRandomWords(char *w){
- /*
- get x amount of random words
- from sorce store in an array
- for later use,return type should
- then be changed, or print
- ramdom words from source.
- code goes here.
- It's a work in progress
- */
- size_t sz=strlen(w);
- //printf("%zu\n",sz);
- char *token;
- int c=0,ct,ct2=0;
- ct=random_int(2,32);
- printf("ran %d\n\n",ct);
- token=strtok(w, " ");
- while(token != NULL){
- for(int i=0;i<ct;i++){
- token=strtok(NULL, " ");
- }
- if(c==5){
- printf("%s\n",token);
- c=0;
- ct2++;
- }
- if(ct2==5){
- printf("\n");
- ct2=0;
- }
- token=strtok(NULL, " ");
- c++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement