读取文件的功能不起作用。交互菜单问题
Function that reads the file doesn't work. Interactive menu problem
我在使新开始的项目运行时遇到了一些问题(我也是初学者)。
由于某种原因,我的交互式菜单中的选项 4 不起作用,只采用默认路由(不输出文件中的内容(文件目录很好。)。
此时我已经阅读了每个论坛以寻找答案,但无法以任何可行的方式修改我的代码。
所以我决定向你求助。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define kFileLocation "/Users/patrykpiwowarczyk/Desktop/STUDIA/FoCP/Kodowanie/TestProjektSemestralnyAngielski/TestProjektSemestralnyAngielski/authors.txt"
void options();
void start(void);
void score(void);
void optionz(void);
void author(void);
void pexit(void);
int main(void)
{
char ch;
int num;
char option;
while (1) {
printf("****Your English Learning Index Cards****\n\n");
printf("Enter 1-5 of the following options: \n\n");
options();
scanf("%c", &option);
switch (option) {
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
author();
break;
case '5':
pexit();
break;
default:
printf("Please insert number ranging from 1-5 though... No cheating! \n\n");
printf("Press ENTER key to Continue\n");
}
}
return 0;
}
void options()
{
printf("1. Start Game \n");
printf("2. View Scoreboard \n");
printf("3. Options \n");
printf("4. Author \n");
printf("5. Exit \n\n");
}
void author()
{
char c;
FILE *authorsFile;
if ((authorsFile = fopen("/Users/patrykpiwowarczyk/Desktop/STUDIA/FoCP/Kodowanie/TestProjektSemestralnyAngielski/TestProjektSemestralnyAngielski/authors.txt","r")) == NULL)
{
printf("FAILED to read the file, maybe check directory?\n");
exit(1);
}
while ((c = fgetc(authorsFile)) != EOF)
{
printf("%c", c);
}
fclose(authorsFile);
}
void pexit()
{
puts("Your progress has been saved, see you next time.");
exit(0);
}
如果你能以任何方式帮助我,我将不胜感激..
你好,Patryk Piwowarczyk。
PS:#define kFileLocation 是我其他尝试的遗留物。省略。
根据您的评论,我得出以下结论:
问题是 scanf
在第一次调用时将数字正确写入变量 option
。然而,第二次调用 scanf
时,它立即返回了上一次菜单选择的换行符,而不是等待用户输入另一个数字。每当 scanf
返回换行符时,都会触发默认情况。
因此,将 scanf
调用更改为以下内容可以最好地解决问题:
scanf(" %c", &option);
通过将 space 添加到格式字符串的开头,您指示 scanf
在读取字符之前丢弃所有白色 space 字符。这样,您可以确保永远不会将换行符写入 option
变量。
scanf
读取换行符而不是丢弃它们的问题已在 this question 中进行了更详细的讨论。
我在使新开始的项目运行时遇到了一些问题(我也是初学者)。 由于某种原因,我的交互式菜单中的选项 4 不起作用,只采用默认路由(不输出文件中的内容(文件目录很好。)。
此时我已经阅读了每个论坛以寻找答案,但无法以任何可行的方式修改我的代码。 所以我决定向你求助。 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define kFileLocation "/Users/patrykpiwowarczyk/Desktop/STUDIA/FoCP/Kodowanie/TestProjektSemestralnyAngielski/TestProjektSemestralnyAngielski/authors.txt"
void options();
void start(void);
void score(void);
void optionz(void);
void author(void);
void pexit(void);
int main(void)
{
char ch;
int num;
char option;
while (1) {
printf("****Your English Learning Index Cards****\n\n");
printf("Enter 1-5 of the following options: \n\n");
options();
scanf("%c", &option);
switch (option) {
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
author();
break;
case '5':
pexit();
break;
default:
printf("Please insert number ranging from 1-5 though... No cheating! \n\n");
printf("Press ENTER key to Continue\n");
}
}
return 0;
}
void options()
{
printf("1. Start Game \n");
printf("2. View Scoreboard \n");
printf("3. Options \n");
printf("4. Author \n");
printf("5. Exit \n\n");
}
void author()
{
char c;
FILE *authorsFile;
if ((authorsFile = fopen("/Users/patrykpiwowarczyk/Desktop/STUDIA/FoCP/Kodowanie/TestProjektSemestralnyAngielski/TestProjektSemestralnyAngielski/authors.txt","r")) == NULL)
{
printf("FAILED to read the file, maybe check directory?\n");
exit(1);
}
while ((c = fgetc(authorsFile)) != EOF)
{
printf("%c", c);
}
fclose(authorsFile);
}
void pexit()
{
puts("Your progress has been saved, see you next time.");
exit(0);
}
如果你能以任何方式帮助我,我将不胜感激..
你好,Patryk Piwowarczyk。
PS:#define kFileLocation 是我其他尝试的遗留物。省略。
根据您的评论,我得出以下结论:
问题是 scanf
在第一次调用时将数字正确写入变量 option
。然而,第二次调用 scanf
时,它立即返回了上一次菜单选择的换行符,而不是等待用户输入另一个数字。每当 scanf
返回换行符时,都会触发默认情况。
因此,将 scanf
调用更改为以下内容可以最好地解决问题:
scanf(" %c", &option);
通过将 space 添加到格式字符串的开头,您指示 scanf
在读取字符之前丢弃所有白色 space 字符。这样,您可以确保永远不会将换行符写入 option
变量。
scanf
读取换行符而不是丢弃它们的问题已在 this question 中进行了更详细的讨论。