char **query;
query = (char**) malloc ( sizeof(char*) );
int f=0;
int i=0,j=0,c;
while((c=getchar())!=EOF)
{
if(!isalpha(c))
continue;
if(f==1)
query=(char**) realloc(query,(i+1)*sizeof(char*));
query[i]=(char*) malloc(sizeof(char));
query[i][j]=c;
j++;
while( (c=getchar())!=EOF&&c!=' '&&c!='\t' )
{
query[i]=(char*) realloc(query[i],(j+1)*sizeof(char));
query[i][j]=c;
++j;
}
query[i][j]='\0';
printf("%s\n",query[i]);
if(c==EOF){
break;
}
++i;
f=1;
j=0;
}
Run Code Online (Sandbox Code Playgroud)
我希望上面的代码片段读取由空格和制表符分隔的字符串行,直到ONE EOF,但它需要2个EOF才能结束循环.此外,字符串只能由字母字符组成.
我在约2天内挣扎.请提供一些反馈.
编辑:最有可能的原因是我写完最后一个字符串而不是输入键后我按CTRL + D键,但现在我按Enter键然后按CTRL + D,它按预期工作.但是,在按下最后一个字符串后按CTRL + D后,如何将其更改为完成?
在我正在研究的函数中发现的一段代码让我感到困惑:
char GetCommand( void )
{
char command;
do {
printf( "Enter command (q=quit, n=new, l=list): " );
scanf( "%c", &command );
Flush();
}
while ( (command != 'q') && (command != 'n')
&& (command != 'l') );
printf( "\n----------\n" );
return( command );
}
void Flush( void ) {
while ( getchar() != '\n' )
;
}
Run Code Online (Sandbox Code Playgroud)
我在这里不太明白的是该Flush()功能的用法.我的意思是,我正在阅读的书通过说它阻止用户输入多个单个字符然后在第二次提示输入时读取该字符来解释它.
我不明白的是如何Flush()防止这种情况发生.它什么都不做.它只是一个while命令.(虽然这是真的......什么?????)没有意义.
我已经阅读了大约5-10个不同的建议如何清除标准输入,但它们都不适合我的需要.事情就是fflush(stdin)在我的计算机上完美运行,但不幸的是它似乎无处不在,所以我需要具有相同功能的东西.我想尽其他办法清除标准输入时,它不是空的,但需要用户输入时标准输入是空的,这意味着它需要在一个时刻,我不想得到任何输入(+后将其丢弃也无妨).
问题是:stdin在我需要用户输入之前,我可以以某种方式确定IS是空的吗?(如果不是,那么,然后才以某种方式清除它?)类似于:
if (stdin is NOT empty)
while (getchar() != '\n')
continue;
Run Code Online (Sandbox Code Playgroud)
编辑:问题是我stdin逐个加载字符,在某些时候,上一次迭代的输入的一部分可能会或可能不会被丢弃.无论哪种方式,stdin在我要求用户处理另一个输入之前我需要清楚.清除缓冲区本身是没有这样一个大问题,问题是当输入为空当程序到达空地的点会发生什么stdin,因为在那一刻程序需要另一个输入这是会被清除功能被吃掉.这就是我想要摆脱的东西.(当我可以使用时,fflush(stdin);我才知道,对于我的程序的下一行,stdin无论如何,WILL都是空的,没有问题......)
我自己开始学习编程(C)作为一种爱好.我正在使用K&R.
main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
Run Code Online (Sandbox Code Playgroud)
验证getchar()!= EOF是0还是1
我想我明白发生了什么:
但是,我的解决方案是错误的,所以显然我不明白:
main ()
{
int c;
while ((c = getchar()) != EOF)
printf("%d\n", c);
}
Run Code Online (Sandbox Code Playgroud)
这只是打印字符的值.如果按回车键,也会打印"10".
我以为它会打印c.但是,它是打印字符的值而不是1或0值.
我知道在将其与EOF进行比较后,c被赋予1或0.但我不确定我可以使用什么逻辑来表明这一点.看来我需要以某种方式"退出"显示字符值,而是显示比较值.这是否意味着我需要离开while循环?如果是这样,我不知道如何(这只是猜测).
我怎样才能简单地验证c = 1还是0?
而且,我怎么知道这个?我想,必须有一些基本的东西,我应该从中学习.
main ()
{
int c;
while ((c = getchar()) != EOF != 0 != 1)
putchar(c);
}
Run Code Online (Sandbox Code Playgroud)
我也这样做了,我认为这似乎有效.因为它不输出任何字符,但我不确定这是否是他们正在寻找的解决方案......
如果我在标准输入流中键入"Hello World"字样,该程序将打印出奇怪的盒子符号,而不是预期的"Hello World",返回标准输出.
#include <stdio.h>
int main(void)
{
// print out all characters from the stream until '/n' character is found
int ch;
while (ch = getchar() != '\n')
{
putchar(ch);
}
putchar('\n');
}
Run Code Online (Sandbox Code Playgroud)
我知道如何解决这个问题.但为什么这行代码不正确?
while (ch = getchar() != '\n')
Run Code Online (Sandbox Code Playgroud) 我的主要目的是在getchar获得角色后立即返回,而不是等待ENTER密钥.我试过这个
int main()
{
setvbuf(stdin,NULL,_IONBF,0);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将其与原型进行比较 setvbuf
setvbuf ( FILE * stream, char * buffer, int mode, size_t size );
Run Code Online (Sandbox Code Playgroud)
它应该设置stdin为无缓冲模式.
但仍然getchar()在等待ENTER
我见过这样的相关帖子
这表明制作stdin无缓冲的替代方法.但我很想知道为什么setvbuf方法不起作用
我想在 python 中进行用户输入,这类似于c++ 中使用的getchar()函数。
C++代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char ch;
while(1){
ch=getchar();
if(ch==' ') break;
cout<<ch;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输入:堆栈溢出
输出:堆栈
在上面的代码中,当用户输入一个空格时,循环会中断。我想使用我在 c++ 代码中使用的getchar()类型函数在 python 中执行此操作。
我编写控制台应用程序,为int执行几个scanf,然后执行getchar:
int x,y;
char c;
printf("x:\n");
scanf("%d",&x);
printf("y:\n");
scanf("%d",&y);
c = getchar();
Run Code Online (Sandbox Code Playgroud)
因此,我得到了c = '\n',尽管输入是:
1
2
a
Run Code Online (Sandbox Code Playgroud)
这个问题怎么解决?
我在将单元测试写入涉及IO操作的C函数时面临问题.例如,下面是我编写的代码,用于从控制台获取用户的输入字符串.我不知道如何使用getchar()函数自动测试用户输入.
char * GetStringFromConsole()
{
char *strToReturn = NULL;
int len = 128;
strToReturn = (char*)malloc(len);
if (strToReturn)
{
int ch;
char *ptr = strToReturn;
int counter = 0;
for (; ;)
{
ch = getchar();
counter++;
if (counter == len)
{
strToReturn = realloc(strToReturn, len*=2 );
ptr = strToReturn + counter-1;
}
if ((ch != EOF) && (ch != '\n') && (counter < len))
{
*ptr++ = ch;
}
else
{
break;
}
}
*ptr = '\0';
}
return …Run Code Online (Sandbox Code Playgroud) 我目前在1.5.1文件复制中,并制作了一个类似的程序:
#include <stdio.h>
/* copy input to output; 1st version */
main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我像这样运行它:
PS <..loc..> cc copy-0.c
PS ./a
Black
Black
White
White
Gray
Gray
Run Code Online (Sandbox Code Playgroud)
输出是我输入的.
这是我为实验目的而制作的一个程序:
#include <stdio.h>
/* copy input to output; 1st version */
main()
{
int c;
c = getchar();
while (c != EOF) {
printf("%c",c);
c = getchar();
}
}
Run Code Online (Sandbox Code Playgroud)
它产生相同的结果但是putchar和之间存在差异printf吗?
两者之间哪个更好用?