简体   繁体   English

Scanf不接受第二个字符字符串

[英]Scanf not accepting second char string

I have a binary search function that will search for a word in an array, but before I can search the array I need to know what word to search for. 我有一个二进制搜索功能,可以在数组中搜索单词,但是在搜索数组之前,我需要知道要搜索的单词。 I have written the code to ask the user for input, but the program prints out the request for input but doesn't accept anything from the user. 我已经编写了代码来询问用户输入,但是程序会打印出输入请求,但不接受用户的任何东西。 I was thinking it was a buffer issue, as I have an initial scanf in the program that loads all the character strings from an external file and places them in an array. 我当时以为这是一个缓冲区问题,因为我在程序中有一个初始scanf,它从外部文件加载所有字符串并将它们放置在数组中。 I have tried using fflush after my initial scanf, and I tried rewriting the second one with gets, as pointed out in previous threads. 我尝试在初始scanf之后使用fflush,并尝试使用gets重写第二个,如先前线程中所指出。 Perhaps I am not implementing it correctly. 也许我没有正确实现它。 Here's what I have so far, any tips as to why the second scanf isn't working is appreciated. 这是我到目前为止的内容,对于第二个scanf为何不起作用的任何提示,我们深表感谢。

#include "set.h"
#include "sortAndSearch.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(){
char names[320][30];
char str[30];  
int i, j;
char *key;
int numOfWords;
char userWord[30];
Set set1, set2, set3;

//scan each char string into array names
for(i=0; scanf("%s", str) != EOF; i++){
        strcpy(names[i], str);
}

//set number of words in file
numOfWords = i;

//sort names array
//bubbleSort(names, numOfWords);

//print out names, sorted
//for(i=0; i<numOfWords; i++){
//      printf("%s\n", names[i]);
//}

printf("What word would you like to search for? ");
scanf("%s", userWord);

//addName2Set(set1, userWord);

return 0;
}

Your second scanf doesn't work because your first scanf never terminates. 您的第二个scanf无效,因为您的第一个scanf永不终止。 scanf won't return EOF unless the input stream is closed - that would be that the console closes. 除非关闭输入流,否则scanf不会返回EOF这可能是控制台关闭了。

Since scanf returns the number of characters read you should instead make your loop condition scanf(%s, str) != 0 . 由于scanf返回读取的字符数,因此您应该使循环条件scanf(%s, str) != 0 That will make the loop end as soon as the user hits enter without entering anything. 这将使循环在用户未输入任何内容的情况下立即结束。

Your initial scanf() in a loop read everything up to EOF, so there's nothing left for the 'What word would you like to search for?' 循环中的初始scanf()读取EOF之前的所有内容,因此“想搜索什么单词?”一无所有。 scanf() to read. scanf()读取。

One way around this problem is to read the initial names from a file ( fopen() , fscanf() , fclose() — and the file name might be an argument to the program, or a fixed name). 解决此问题的一种方法是从文件中读取初始名称( fopen()fscanf()fclose() ,并且文件名可能是程序的参数,也可能是固定名称)。

Another way you could try is clearerr(stdin); 您可以尝试的另一种方法是clearerr(stdin); before the 'What word' scanf() . 在'What scanf() That ( clearerr() ) unsets the EOF bit and allows scanf() to try again. 该( clearerr() )取消设置了EOF位,并允许scanf()再次尝试。 It may work if the input of the program is the terminal; 如果程序的输入是终端,则可能会起作用。 it won't help if the input of the program is coming from a file. 如果程序的输入来自文件,则无济于事。


int main(int argc, char **argv)
{
    char names[320][30];
    char str[30];  
    int i, j;
    char *key;
    int numOfWords;
    char userWord[30];
    Set set1, set2, set3;
    FILE *fp;

    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s word-file\n", argv[0]);
        exit(1);
    }

    if ((fp = fopen(argv[1], "r")) == 0)
    {
        fprintf(stderr, "Usage: %s word-file\n", argv[0]);
        exit(1);
    }

    //scan each char string into array names
    for (i = 0; i < 320 && fscanf(fp, "%29s", str) != EOF; i++)
    {
        strcpy(names[i], str);
    }

    fclose(fp);

    //set number of words in file
    numOfWords = i;

This insists that you use ./a.out example.dat (instead of ./a.out < example.dat ). 这坚持要求您使用./a.out example.dat (而不是./a.out < example.dat )。 It will then work more or less as you want it to. 然后,它会或多或少地发挥您的作用。 Of course, the code for reading the file should be in a function that is passed the file name, the array, and the array size. 当然,用于读取文件的代码应该在传递了文件名,数组和数组大小的函数中。 The 320 in the loop is overflow protection and should be an enumeration enum { MAX_WORDS = 320 }; 循环中的320为溢出保护,应为枚举enum { MAX_WORDS = 320 }; that's used both in the array declaration and the loop. 在数组声明和循环中都使用了它。 The 29 is overflow protection; 29是溢出保护; it is hard to parameterize that, but it is one less than the second dimension of the array. 很难对其进行参数化,但是它比数组的第二维小一。

Your question and code seemed awfully misaligned at first glance... 乍一看,您的问题和代码似乎完全错位了……

scanf() is the wrong function for reading from your file, you want fscanf() , or if the file is formatted such that each word is on its own line fgets() works too (newline stops reading of each string). scanf()是错误的函数,您想要从文件中读取文件,您想要fscanf() ,或者如果文件格式被格式化为每个单词都在其fgets()行上,则fgets()可以工作(换行停止读取每个字符串)。 Likewise, you can use gets() instead of scanf() to read user input if the input is just a string followed by return(newline). 同样,如果输入只是一个字符串,然后是return(newline),则可以使用gets()而不是scanf()来读取用户输入。

Everything you need to know about stdio.h 您需要了解有关stdio.h的所有信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM