C Programming - Issue using my Random Number Generator to read line from text file

This is a discussion on C Programming - Issue using my Random Number Generator to read line from text file within the C Programming forums, part of the General Programming Boards category; Hey. So I am not very good at C Programming and need a little help. I have written a random ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    C Programming - Issue using my Random Number Generator to read line from text file

    Hey. So I am not very good at C Programming and need a little help.
    I have written a random number generator program which I need to use to randomly pick a word from my dictionary text file. However, I am not sure how to write the part that retrieves the word from the text file. Here is what i have so far. Any help would be greatly appreciated.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int RandomWordSelection(void){
        //picks a random number between 1 and the max_words in the dictionary file
        
        int max = 50;
        int i, number;        
                
        srand(time(NULL));
        for(i = 0; i <= 50; i++);
            number = ((float)rand())/RAND_MAX*50+1;
        
    return number;
    }
    
    
    
    
    char GetsWord(void){
    //Gets the number word from the dictionary that was selected by 
    // the random number generator    
        int i, x;
        char SecretWord;
        int RandomWordSelection(void);
        i = RandomWordSelection();
        
        FILE*fptr = fopen("dictionary.txt", "r");
        
    //Not sure how to proceed at this point
        
        
        
    return SecretWord;
    }

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    107
    Quote Originally Posted by Klcastillo View Post
    Code:
    //picks a random number between 1 and the max_words in the dictionary file
    I don't see any max_words in your code but if you do define it you can use rand() like this
    Code:
    number = rand() % max_words + 1;
    Quote Originally Posted by Klcastillo View Post
    However, I am not sure how to write the part that retrieves the word from the text file
    It depends on the format of the file - what is the character separating words in the file (space, coma, end-of-line)?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    First things first:
    Take this line out of RandomWordSelection and instad put it inside main, right at the top:
    Code:
    srand(time(NULL));
    Second, stop programming by writing a bunch of random things and then hoping those things do something useful.
    Instead, start small, form a program that only has things in it that you are sure you fully understand. Then gradually add bits one at a time. Don't proceed to add another bit until you understand the previous bit.
    Look up "fscanf", and "fclose". Read about how to determine if the "file was opened successfully". Learn about "arrays". Learn about "null-terminated strings".
    You don't need to generate 102 random numbers to pick a single random word from a file.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    File Format : The dictionary file contains one word per line.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    The dictionary file is formatted so that there is one word per line.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by Klcastillo View Post
    File Format : The dictionary file contains one word per line.
    Okay.

    Quote Originally Posted by Klcastillo View Post
    The dictionary file is formatted so that there is one word per line.
    No way...

    ------

    But in all seriousness, look into fgets(3): input of char/strings - Linux man page.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Read a random line from a text file
    By RyanLeonard in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2011, 03:57 AM
  3. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 09:51 AM
  4. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  5. Read from file, output random line
    By Asbestos in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2005, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21