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
    Posts
    9

    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

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    #include<time.h>
    #include<stdlib.h>
    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;
    	
    	for(i=0;i<15;i++)	
    	{
    		strcpy(temporary,"....");
    		rand=function();
    		
    		for(q=0;q<strlen(setstrings[rand]);q++)
    		{
    			present[q]=setstrings[rand][q];
    		}
    		
    		do
    		{
    			printf("\n\nYou Have %i Tries Remaining\n"
    				"Press The Desired Alphabet Key To Guess Your Answer\n\n",addup);
    			printf(temporary);
    			ch=getch();
    			mark = 2;
    			for(t=0;t<strlen(present);t++)
    			{
    				if(ch==present[t])temporary[t]=ch;
    				mark=0;
    			}
    			if(mark<2)addup--;
    			if(addup<1){
    				mistake();
    				return 0;
    			}
    		}
    		while(strcmp(temporary,setstrings[rand]));
    		printf("\nYou Have Completed The Word Successfully\n"
    			"Press Any Key To Try Another Word With The Tries Remaining");
    		ch=getche();
    	}
    	return 0;
    }
    
    int function(void)
    {
    	srand((unsigned)time(NULL));
    	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
    Posts
    473
    a very simple method would be:
    Code:
    #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-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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