Jay*_*ind 3 c struct pointers scanf fgets
以下是我的代码中的代码段.我学会了更好地使用fscanf而不是scanf.但fscanf并没有等待输入
switch (argc) {
case 2: printf("\nEnter the subject code: ");
while(fgets(temp_op->subject, BUF_NOTES, stdin)==NULL);
case 3: printf("\nEnter the topic: ");
while(fgets(temp_op->topic, BUF_TOPIC, stdin)==NULL);
case 4: printf("\nEnter the Level: ");
flag = fscanf(stdin,"%d",&temp_op->level);
case 5: printf("\nEnter the Answer Key: ");
while(fgets(temp_op->key, BUF_KEY, stdin)==NULL);
case 6: printf("\nEnter any additional notes(optional): ");
while(fgets(temp_op->notes, BUF_NOTES, stdin)==NULL);
break;
default:printf("\nExcess Arguments");
}
Run Code Online (Sandbox Code Playgroud)
问题在于case 5.fgets不是在等待输入,但是案例6表现良好.
但是,如果我注释掉case 4"flag = ..." 这一行,那么下一个fgets将提示输入.奇怪的.我想知道为什么以前的fscanf影响后者的fgets.我的结构定义是:
typedef struct {
int mode ;
int level;
char subject[BUF_SUBJECT], topic[BUF_TOPIC], notes[BUF_NOTES], key[BUF_KEY];
} operation;
Run Code Online (Sandbox Code Playgroud)
完整的资料来源是http://pastebin.com/HVvGC3B7
可能有什么不对?
你混合scanf()使用fgets()最好避免- .
fscanf(stdin,"%d",...离开\n输入队列,以下fgets()消耗而不等待额外的输入.
建议使用fgets()thoguhout并使用sscanf(buffer, "%d", ...获取整数.
| 归档时间: |
|
| 查看次数: |
7017 次 |
| 最近记录: |