我在尝试通过我的代码获取用户输入时遇到了一个奇怪的问题。我很确定问题不在于代码,而与标准输入流 (stdin
) 或类似的东西等操作系统有关,但由于我没有另一台具有类似操作系统设置的机器(因为现在几乎不可能找到 SCO 机器),我希望一些编程解决方法来解决这个问题。我的程序从由'n'
终止的用户读取字母数字字符流。
但是无论我如何尝试通过不同的方式实现这一目标,它都只接受最初的 256 个字符。最初我怀疑问题出在fgets
函数上,但是当我使用 尝试使用fgets
从文件中读取相同的值时,它按预期工作。
方法1:
main()
{
char szInLine[999];
memset(szInLine, 0, sizeof(szInLine));
fprintf(stdout, "nPlease enter the encrypted value:n");
if (fgets(szInLine, 997, stdin) == NULL)
return(1);
fprintf(stdout, "Encrypted data string contains %i characters: %sn",
strlen(szInLine), szInLine);
}
方法2:
while(ch = getc(stdin)) != EOF)
{
if((*szInLine++ = ch) == 'n')
{
break;
}
}
*szInLine = ' ';
fprintf(stdout, "Encrypted data string contains %i characters: %sn", strlen(szInLine), szInLine);
两种情况的输出:"加密数据字符串包含 256 个字符:abcde.....
我已经尝试但没有成功的其他方法包括更改保存值的缓冲区的数据类型(从字符串到unsigned long
),动态地为缓冲区分配内存,将stdin
设置为无缓冲等。
操作系统环境: SCO Unix,32位 编译器: 抄送
参见上海合作组织网站上的 ioctl() 和 stty() 手册页。您应该能够通过测试终端与重定向来检索设置的差异。
好吧,你的程序(两者都)有错误:
/* you should include <stdio.h> so fgets() can return a char *,
* If you don't, it's assumed fgets() returns an int value. */
#include <stdio.h>
main()
{
char szInLine[999];
memset(szInLine, 0, sizeof(szInLine)); /* you don't need this */
fprintf(stdout, "nPlease enter the encrypted value:n");
/* fgets accepts a buffer and its size, it will reserve space for
* one ' ' char. */
if (fgets(szInLine, sizeof szInLine, stdin) == NULL) {
/* it is good to print some diagnostic if you receive EOF */
return(1);
}
fprintf(stdout, "Encrypted data string contains %i characters: %sn",
strlen(szInLine), szInLine);
/* you should return 0, here */
return(0);
}
第二个更糟糕:
/* unbalanced parenthesis, you lack a parenthesis after 'while' keyword */
while(ch = getc(stdin)) != EOF)
{
if((*szInLine++ = ch) == 'n')
{
break;
}
}
*szInLine = ' ';
/* if you move the pointer 'szInLine' it will always be pointing to the end of
* the string, so this printf will show 0 characters and an empty string, you
* had better to save the pointer at the beginning, so you don't lose the
* reference to the string beginning.
*/
fprintf(stdout, "Encrypted data string contains %i characters: %sn", strlen(szInLine), szInLine);
这应该有效:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char buffer_in[1000];
char buffer_out[1000];
while (fgets(buffer_in, sizeof buffer, stdin)) {
/* you'll get a line of up to 'sizeof buffer_in - 1' chars with an
* ending 'n' (or a truncated if the line has more than 'sizeof
* buffer_in - 1' chars. Also, you'll have a 'n' at the end of the
* buffer, if the line filled partially the buffer. */
fprintf(stderr,
"String read (%d chars): %s", /* this is why I don't put a 'n' here */
strlen(buffer_in),
buffer_in);
/* encrypt(buffer_in, sizeof buffer_in, buffer_out, sizeof buffer_out); */
}
/* here you got EOF */
return 0;
}
或者如果你想使用getc()
:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
/* it is very important that c be an int, see manual
* page of fgetc(), getch() or getchar() */
int c;
char buffer[1000], *p = buffer;
/* we check for buffer length and for EOF. As we are doing the hard
* work ourselves, we have to check for 'sizeof buffer - 1' to allow
* space for the ' '. */
while ((p < buffer + sizeof buffer - 1) && ((c = getchar()) != EOF)) {
if (c == 'n') { /* A NEWLINE, act on buffer, and skip it. */
*p = ' '; /* end the string */
printf("Read %d chars: %sn", p - buffer, buffer);
/* crypt it ... */
/* ... */
p = buffer; /* reset buffer */
continue;
}
*p++ = c; /* add the character to the buffer */
}
/* here you got EOF */
return 0;
}
最后一点:
不要发布代码片段,而是完整的示例,因为很难确定哪些错误是在此处复制代码时的错误,或者哪些是您在原始程序中犯的错误。