这是为什么? [英] Why is that?

查看:71
本文介绍了这是为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道造成这个问题的原因是什么,但我无法找到原因。

#define newline" \ n"

#define question_mark" \?"

#define single_quotas" \''"

#define double_quotas" \""

#define form_feed" \f"

#define horizo​​ntal_tab" \t"

#define vertical_tab" \v"

#define反斜杠" \\"

#include< stdio.h>


main(){

int var;

printf("键入数字:");

scanf(" ;%d",& var);

printf(" the number is");

printf("%d",var);

printf(换行符);


char * var2;

printf(" Type a string:");

int bytes_read = 0;

int nbytes = 100;


var2 =(char *)malloc(nbytes + 1);

bytes_read = getline(& var2,& nbytes,stdin);

if(bytes_read == -1)

{puts(" ERROR!:invalid)输入");}


printf(字符串是);

printf("%s",var2);

printf(换行);

}

I know what is causing the problem, but I couldn''t
find out why.

#define newline "\n"
#define question_mark "\?"
#define single_quotas "\''"
#define double_quotas "\""
#define form_feed "\f"
#define horizontal_tab "\t"
#define vertical_tab "\v"
#define backslash "\\"
#include <stdio.h>

main(){
int var;
printf("Type a number:");
scanf("%d",&var);
printf("The number is ");
printf("%d",var);
printf(newline);

char *var2;
printf("Type a string:");
int bytes_read=0;
int nbytes = 100;

var2 = (char *) malloc (nbytes + 1);
bytes_read = getline (&var2, &nbytes, stdin);
if(bytes_read == -1)
{puts ("ERROR!: invalid input");}

printf("The string is ");
printf("%s",var2);
printf(newline);
}

推荐答案

Hi Profetas,
Hi Profetas,

我知道导致问题的原因,但我无法找出原因。


非常有启发性。您认为您的问题是什么?

您的代码甚至无法编译...

#define newline" \\\
"
#define question_mark" ; \?"
#define single_quotas" \''"
#define double_quotas" \""
#define form_feed" \f"
#define horizo​​ntal_tab" \t"
#define vertical_tab" \v"
#define backslash" \\"


我们不会对这些宏的含义或无意义发表评论。

除了新行之外,你不需要那些东西。请

保持你的例子最小化。

#include< stdio.h>


在哪里< stdlib.h> malloc()需要吗?

非标准getline()

函数的标题在哪里? (见下文)


main(){
int main()

int var;
printf(" Type数字:);
scanf("%d"& var);
printf(" number is);
printf("%d" ,var);
printf(换行符);

char * var2;
printf(" Type a string:");
int bytes_read = 0;
int nbytes = 100;

var2 =(char *)malloc(nbytes + 1);
你已经在这里足够长了:不要施放malloc()的

结果。顺便说一句:如果你想申请一个演员,

然后施放nbytes - malloc需要一个size_t参数。 bytes_read = getline(& var2,& nbytes,stdin);来自''男人获取''的


#define _GNU_SOURCE

#include< stdio.h>


ssize_t getline(char ** lineptr,size_t * n,FILE * stream);

我不知道返回类型ssize_t但是其余的manpage

似乎告诉我它是fgets()的改进版本,比如我们大多数人都在他们的私人图书馆中使用的b $ b ...

我只会让var2 = NULL - 因为getline正在处理

分配,这对于一个简短的程序来说没问题。

if(bytes_read == -1)
bytes_read不是ssize_t类型。 {puts(错误!:无效输入);}
这可能是另一个问题
printf(字符串是);
printf("%s", var2);
printf(换行符);
}
I know what is causing the problem, but I couldn''t
find out why.
Very enlightening. What do you think your problem is?
Your code does not even compile...
#define newline "\n"
#define question_mark "\?"
#define single_quotas "\''"
#define double_quotas "\""
#define form_feed "\f"
#define horizontal_tab "\t"
#define vertical_tab "\v"
#define backslash "\\"
Won''t comment on the sense or nonsense of these macros.
Apart from the newline, you do not need that stuff. Please
keep your examples minimal.
#include <stdio.h>
Where is <stdlib.h> needed for malloc()?
And where is the header for the non-standard getline()
function? (see below, too)

main(){ int main ()
int var;
printf("Type a number:");
scanf("%d",&var);
printf("The number is ");
printf("%d",var);
printf(newline);

char *var2;
printf("Type a string:");
int bytes_read=0;
int nbytes = 100;

var2 = (char *) malloc (nbytes + 1); You have been around here long enough: Do not cast the
result of malloc(). Btw: If you want to apply a cast,
then cast nbytes -- malloc takes a size_t argument. bytes_read = getline (&var2, &nbytes, stdin); from ''man getline'':
#define _GNU_SOURCE
#include <stdio.h>

ssize_t getline(char **lineptr, size_t *n, FILE *stream);
I do not know the return type ssize_t but the rest of the manpage
seems to tell me that it is an improved version of fgets() such
as most of us have in their personal library...
I would just make var2=NULL -- as getline is taking care of
allocation, this is alright for a short program.
if(bytes_read == -1) bytes_read is not of type ssize_t. {puts ("ERROR!: invalid input");} It might be another problem
printf("The string is ");
printf("%s",var2);
printf(newline);
}



free()var2。除此之外:为什么使用printf()给予

a''\ n''。


请生成编译为
$的代码b $ b gcc -ansi -pedantic

然后回来。

- 迈克尔


free() var2. Apart from that: Why use printf() for giving
a ''\n''.

Please produce code that compiles for
gcc -ansi -pedantic
and then come back.
--Michael


在我忘记它之前:


#define _GNU_SOURCE

#include< stdio.h>

#include< stdlib .h>


#define换行符" \ n"


int main(无效)

{

int var;

char * var2 = NULL;

ssize_t bytes_read = 0;

size_t nbytes = 0 ;

printf("键入数字:"); (void)fflush(stdout);

bytes_read = getline(& var2,& nbytes,stdin);

if(bytes_read == -1)

{

fprintf(stderr," ERROR in line%d in return value from"

" getline()" newline,__LINE __);

(无效)fflush(stderr);

退出(EXIT_FAILURE);

}

if(sscanf( var2,"%d"& var)!= 1)

{

fprintf(stderr,行%d中的错误:scanf()不能挤出

你的输入中的一个数字换行符,__LINE__;

(无效)fflush(stderr);

退出( EXIT_FAILURE);

}

printf(数字是%d换行符,var);


printf(" ;键入一个字符串:"); (void)fflush(stdout);

bytes_read = getline(& var2,& nbytes,stdin);

if(bytes_read == -1)

{

fprintf(stderr," ERROR in line%d in return value from"

" getline()" newline,__LINE __);

(无效)fflush(stderr);

退出(EXIT_FAILURE);

}


printf(" string is%s" newline,var2); (void)fflush(stdout);


免费(var2);

退出(EXIT_SUCCESS);

}

为我们这些有足够新gcc编译器的人完成这项工作。


你的问题当然是由我取代的部分

getline()/ sscanf()对。

其他人:想想fgets()/ sscanf()和分配的缓冲区

var2。

至于原因:scanf()和系列只是没有像你期望的那样清空

输入缓冲区,而是只得到他们想要的

。 getline()找到了剩下的''\ n'',那就是

。所以,如果你想用scanf()来做,你将用

用s.th来清空输入缓冲行。喜欢


而((c = getchar())!= EOF)

if(c ==''\ n'')

休息;


(未经测试)


干杯

Michael

Before I forget it:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

#define newline "\n"

int main (void)
{
int var;
char *var2 = NULL;
ssize_t bytes_read = 0;
size_t nbytes = 0;
printf("Type a number: "); (void) fflush(stdout);
bytes_read = getline (&var2, &nbytes, stdin);
if(bytes_read == -1)
{
fprintf(stderr,"ERROR in line %d in return value from"
" getline()" newline, __LINE__);
(void) fflush(stderr);
exit(EXIT_FAILURE);
}
if (sscanf(var2, "%d", &var) != 1)
{
fprintf(stderr,"ERROR in line %d: scanf() could not squeeze"
" a number out of your input" newline, __LINE__);
(void) fflush(stderr);
exit(EXIT_FAILURE);
}
printf("The number is %d" newline, var);

printf("Type a string: "); (void) fflush(stdout);
bytes_read = getline (&var2, &nbytes, stdin);
if(bytes_read == -1)
{
fprintf(stderr,"ERROR in line %d in return value from"
" getline()" newline, __LINE__);
(void) fflush(stderr);
exit(EXIT_FAILURE);
}

printf("The string is %s" newline, var2); (void) fflush(stdout);

free(var2);
exit(EXIT_SUCCESS);
}
does the job for those of us with a new enough gcc compiler.

Your problem, of course, was the part I replaced by the
getline()/sscanf() pair.
Everyone else: Think fgets()/sscanf() and allocated buffer
var2.
As to the why: scanf() and family just do not empty the
input buffer as you seem to expect but get only what
they want. getline() found the leftover ''\n'' and that was
it. So, if you want to do it with scanf(), you will have
to empty the input buffer line with s.th. like

while ( (c=getchar()) != EOF )
if (c==''\n'')
break;

(not tested)

Cheers
Michael


Profetas写道:
Profetas wrote:

我知道是什么导致了这个问题,但我无法找出原因。

I know what is causing the problem, but I couldn''t
find out why.



这是哪个问题?


Brian Rodenborn


Which problem is that?

Brian Rodenborn


这篇关于这是为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆