我在scanf冻结时遇到问题。我环顾四周,尽管有些问题相似,但它们并没有帮助解决我的问题。
main(int argc, char **argv) {
//FILE *stream = stdin;
if (stdin == NULL) {
printf("Could not open file");
return 0;
}
int exists = 0;
char letter;
char next = 'H';
char word[30];
int frequency = -1;
int sample = -1;
char *channels;
channels=malloc(sizeof(7*sizeof(char)));
int bitres = -1;
int secondE = 0;
while (exists == 0) {
scanf("%c", &letter); //this is the problem, possibly scanf
printf("AFTER");
if (letter == EOF) {
// printf(letter);
printf("HEADER NOT DETECTED");
return 0;
}
我已经使用printf查明了问题。我目前正在通过命令提示符将另一个文件传递到该程序中。当我到达scanf时,它挂起了。如果有人知道解决方案,我将非常感激。
附带说明一下,使用scanf不好吗?将stdin分配给文件指针也很容易(实际上我已经注释掉了),但是scanf似乎也很容易。
最佳答案
你冻结是什么意思我运行这段代码。当您的代码到达scanf
时,它将等待您的输入。您提供一些输入,然后看看会发生什么。
scanf并不是一个坏习惯。
channels=(char*)malloc(sizeof(7*sizeof(char)));
int bitres = -1;
int secondE = 0;
while (exists == 0)
{
scanf("%c", &letter); // ok
printf("AFTER");
printf("\n");
printf("%c", letter);
printf("\n");
if (letter == EOF) {
// printf(letter);
printf("HEADER NOT DETECTED"); }
return 0;
}
scanf也永远不会返回EOF。