Reading From File.

This is a discussion on Reading From File. within the C Programming forums, part of the General Programming Boards category; Hi I am working on a program which randomly selects a word from a list, hides it from the user ...

  1. #1
    Registered User
    Join Date
    Apr 2004

    Reading From File.

    Hi I am working on a program which randomly selects a word from a list, hides it from the user and then asks the user to guess each character, revealing each character as the user gets it right. At the same time counting down the tries the user has had guessing.

    I have completed the program, however I dont want to limit myself on the words already given in the program to the user. I want to read from a file and then randomly select a word and from their continue the process.

    Please could some one help me out how to go forward using the reading from file and also choosing a random word from the file.

    Much Appreciated.

    Here is the source code

    int function(void);
    void mistake(void);
    void greet(void);
    int main(void)
    	char setstrings[5][5] = {"note", "hell", "love", "gift"};
    	char temporary[5] = "....";
    	int addup, rand, mark;
    	char present[5];
    	char ch;
    	int i,t,q;
    	addup = 25;
    	mark = 2;
    			printf("\n\nYou Have %i Tries Remaining\n"
    				"Press The Desired Alphabet Key To Guess Your Answer\n\n",addup);
    			mark = 2;
    				return 0;
    		printf("\nYou Have Completed The Word Successfully\n"
    			"Press Any Key To Try Another Word With The Tries Remaining");
    	return 0;
    int function(void)
    	return (int)rand()%10;
    void mistake(void)
    	printf("\n\nSorry, But You Have Run Out Of Your Tries. Please Retry The Program");

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    a very simple method would be:
    #include <stdio.h>
    #include <time.h>
    char words[100][BUFSIZ];
    int r;
    srand((unsigned int)time(NULL));
    r = rand() % 100; /* choose a number between 0 and 99 (inclusive) */
    FILE *fp = fopen("file.wds","rt");
    fread(words,sizeof(words),1,fp); /* read in 100 words... */
    printf ("%s\n",words[r]); /* print random word */
    this has some severe limitations of course, you might want to instead read in each word in a loop, for dynamics. Also its missing error checking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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