Detect if the input file cannot be opened
This commit is contained in:
19
hangman.c
19
hangman.c
@@ -135,14 +135,13 @@ void initGuessWord(game_state *gs, char *filename) {
|
|||||||
int i;
|
int i;
|
||||||
mvprintw(0, 1, "Please enter your word: ");
|
mvprintw(0, 1, "Please enter your word: ");
|
||||||
if (strlen(gs->guessWord) == 0) { /* Word can be set by arguments */
|
if (strlen(gs->guessWord) == 0) { /* Word can be set by arguments */
|
||||||
if (strlen(filename) == 0) {
|
/* If a file argument was specified then try to read a random line from the file,
|
||||||
|
if this is not successful or if there is no file argument use the manual input. */
|
||||||
|
if (strlen(filename) == 0 || readRandomLine(filename, gs->guessWord) != 0) { // readRandomLine is only excecuted when strlen(filename) != 0
|
||||||
/* Manual input */
|
/* Manual input */
|
||||||
curs_set(1);
|
curs_set(1);
|
||||||
getnstr(gs->guessWord, MAXWORDLENGTH - 1); /* Reads the guessWord with a limit */
|
getnstr(gs->guessWord, MAXWORDLENGTH - 1); /* Reads the guessWord with a limit */
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
} else {
|
|
||||||
/* Random line from file */
|
|
||||||
readRandomLine(filename, gs->guessWord);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,10 +365,16 @@ int centerDiff(int coordinate, char *str) {
|
|||||||
return coordinate - (len / 2); /* Integer division */
|
return coordinate - (len / 2); /* Integer division */
|
||||||
}
|
}
|
||||||
|
|
||||||
void readRandomLine(char *file, char *result) {
|
int readRandomLine(char *file, char *result) {
|
||||||
FILE *fp = fopen(file, "r");
|
FILE *fp;
|
||||||
int count = 0, wordlength = 0;
|
int count = 0, wordlength = 0;
|
||||||
char line[MAXWORDLENGTH];
|
char line[MAXWORDLENGTH];
|
||||||
|
|
||||||
|
fp = fopen(file, "r");
|
||||||
|
if (fp == NULL) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
while (fgets(line, MAXWORDLENGTH, fp) != NULL) {
|
while (fgets(line, MAXWORDLENGTH, fp) != NULL) {
|
||||||
count++;
|
count++;
|
||||||
@@ -381,6 +386,8 @@ void readRandomLine(char *file, char *result) {
|
|||||||
wordlength = strlen(result);
|
wordlength = strlen(result);
|
||||||
result[wordlength - 1] = '\0';
|
result[wordlength - 1] = '\0';
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void trollHitScreen(game_state *gs, hitfeed *hf, int hits) {
|
void trollHitScreen(game_state *gs, hitfeed *hf, int hits) {
|
||||||
|
@@ -62,7 +62,7 @@ void showStartScreen(game_state *gs);
|
|||||||
void startGame(game_state *gs, hitfeed *hf);
|
void startGame(game_state *gs, hitfeed *hf);
|
||||||
void showHelp(game_state *gs);
|
void showHelp(game_state *gs);
|
||||||
int centerDiff(int coordinate, char *str);
|
int centerDiff(int coordinate, char *str);
|
||||||
void readRandomLine(char *file, char *result);
|
int readRandomLine(char *file, char *result);
|
||||||
void trollHitScreen(game_state *gs, hitfeed *hf, int hits);
|
void trollHitScreen(game_state *gs, hitfeed *hf, int hits);
|
||||||
void addHitToFeed(hitfeed *hf, char *streak, int hit);
|
void addHitToFeed(hitfeed *hf, char *streak, int hit);
|
||||||
void trollHandleImpressive(hitfeed *hf, game_state *gs, int hits);
|
void trollHandleImpressive(hitfeed *hf, game_state *gs, int hits);
|
||||||
|
Reference in New Issue
Block a user