这个buffer没起作用?
char buffer[11];
while(gets(buffer)){
printf("buffer打印: %s\n",buffer);
}
我是新手,如果问题太简单大家不要笑。
环境netbeans+Cygwin
1、我理解的是buffer只能输入10个字符,如果多于10就丢弃,但从程序运行的情况看可以任意输入,为什么?
2、在IDE环境的控制台下可以正确的输出中文,*.exe中午无法正确显示。
[解决办法]
1.gets(buffer)输入遇到\n换行符结束输入,或者输入buffer长度的字符串结束输入。多于10 的不丢弃,只是第11-20个字符在下一次输入读取。
2.不知道是不是系统控制台的原因,这个不清楚。就好像一般情况下linux的终端也不只是中文,需要借助工具来显示中文。
希望可以帮到你。
[解决办法]
gets(buffer)的返回值是什么你清楚吗?不要乱用
[解决办法]
gets函数是不知道你的buffer多大的,所以你输入多的话,会溢出的。这个不怎么好控制,用的时候要小心。
[解决办法]
可能和编译环境相关,建议换成VC60试一试。
[解决办法]
gets()
Get a line from the stdin stream.
char *gets( char *buffer );
Return Value
Each of these functions returns its argument if successful. A NULL pointer indicates an error or end-of-file condition. Use ferror or feor to determine which one has occurred.
Parameter
buffer
Storage location for input string
Remarks
The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character. _getws is a wide-character version of gets; its argument and return value are wide-character strings.
[解决办法]
char buffer[11]; while(1){ fgets(buffer,11,stdin); printf("buffer打印: [%s]\n",buffer); if ('\n'==buffer[0]) break; }
[解决办法]