可以通过scanf读取字符串,然后根据scanf的返回值确认是否读到EOF。
当输入正确是,scanf会返回输入的变量个数,当遇到EOF时,会返回EOF。
于是代码可以写作
while(scanf("%s",str) != EOF)//输入,直到遇到EOF结束。
{
//使用输入的数据str。
}
......
if (gets (str_arr[i]) == NULL)
{
break;
}
......
man gets
......
RETURN VALUE
fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int
or EOF on end of file or error.
gets() and fgets() return s on success, and NULL on error or when end of file occurs while
no characters have been read.
ungetc() returns c on success, or EOF on error.
试试
for (i = 0; i < LIM; i++)
{
for(j = 0; j < LINELIM;j++)
{
str_arr[i][j] == getchar();
if(str_arr[i][j] == '\n')
break;
}
pst[i] = str_arr[i] ;
if (i != 9)
printf ("请输入第[%d]个字符串:", i + 2) ;
}