Hello my fellow programmers -- I am need of your help yet again.
I am making a hangman game for my final C project but I cannot figure out the syntax for reading from a data file.
this is the scenario... I have a data file called
"dictionar.lib" that has a list of words in it.
I want to first check to see if the file exists.. which I did fine..
Now comes the hard part. I want to chose a word from this list using a randomly generated number and return that number to an array variable as the word to guess for the game.
So far my TA's have told me that I have to input the entire file into an array and to test for the end of a word -- which from the data file i suppose would be a 'return' or '/n' .. each time the function finds a carrage return it would determine all the previous letters to be part of an entire word.
So far this is my code: the pr(); pe(); and ps() are formatting functions so just disregard them. see if you can figure out the problem.
int library(void){
char filib[STRINGSIZE];
char word[NUMOFWORDS][WORDSIZE];
char getword[WORDSIZE];
char letter, y;
FILE *dict;
int i=0,j=0, r;
dict=fopen("dictionary.lib", "r");
/* If unable to locate lilbrary do this */
if (dict==NULL){
pe();pr(20);
printf("Read Error : Unable to locate Local Library.\nPlease make sure it is in the same directoy as the game file.");
printf("Would you like to locate the missing file? (y/n) ");
scanf("%c", &y);
if(y=='y'){
for(scanf("%s", filib);
(dict=fopen(filib, "r"))==NULL;
scanf("%s", filib))
{
pe();
ps(5); printf("Cannot open %s for input\n", filib);
ps(5); printf("Re-Enter Local Library name> ");
i++;
if(i==3){
break;
}
}
}
pe(); pr(20); ps(5);
printf("Load Error : Unable to load Local Library");
}
/* end null error */
else if(dict!=NULL){
pe(); pr(20); ps(5);
printf("Local Library Detected. Loading File\n\n");
for(i=0; !EOF; i++){
for(j=0; j!='\n'; j++){
fscanf(dict,"%c",&getword[j]);
printf("%d",j);
word[i][j]=getword[j];
}
}
r=rand()+1;
/* printf("%d", r);*/
for(i=0; i<10; i++){
printf("%s\n", word[i]);
}
fclose(dict);
printf("\nLibrary Unloaded..\n\n");
}
}
/**/
/** end Load Library -----*/
YOUR HELP IS WELCOME AND MUCH APPRECIATED!@