Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SCO Unix操作系统中获取用户输入时出现的问题_C_Unix_Cc_Sco Unix - Fatal编程技术网

在SCO Unix操作系统中获取用户输入时出现的问题

在SCO Unix操作系统中获取用户输入时出现的问题,c,unix,cc,sco-unix,C,Unix,Cc,Sco Unix,我在试图通过代码获取用户输入时遇到了一个奇怪的问题。我很确定问题不在于代码,而是与操作系统类似的标准输入流(stdin)或类似的东西有关,但由于我没有另一台具有类似操作系统设置的机器(因为现在几乎不可能找到SCO机器),我希望有一些编程解决方法来解决这个问题。我的程序从以'\n'结尾的用户处读取字母数字字符流 但无论我如何尝试通过不同的方式实现这一点,它只接受最初的256个字符。起初,我怀疑问题出在fgets函数上,但当我使用时,尝试使用fgets从文件中读取相同的值,其工作方式与预期一致 方法

我在试图通过代码获取用户输入时遇到了一个奇怪的问题。我很确定问题不在于代码,而是与操作系统类似的标准输入流(
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: %s\n", 
  strlen(szInLine), szInLine);
}
方法2:

while(ch = getc(stdin)) != EOF)
{

  if((*szInLine++ = ch) == '\n')
  {
    break; 
  } 
}
*szInLine = '\0';

fprintf(stdout, "Encrypted data string contains %i characters: %s\n", strlen(szInLine), szInLine);
两种情况下的输出:“加密数据字符串包含256个字符:abcde

我已经尝试过但没有成功的其他方法包括更改保存值的缓冲区的数据类型(从string更改为
unsigned long
),动态地为缓冲区分配内存,将
stdin
设置为unbuffered e.t.c

操作系统环境: SCO Unix,32位 编译器: CC

请参阅SCO网站上的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 '\0' 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: %s\n", 
  strlen(szInLine), szInLine);

  /* you should return 0, here */
  return(0);
}
这应该起作用:

#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;
}
最后一点注意:
不要发布代码片段,而是完整的示例,因为很难确定哪些错误是复制此处代码时的错误,或者哪些是您在原始程序中犯的错误。

将输入导入程序时,您是否能够读取超过256个字符?(
/a.out如果从文件重定向时有效,但从tty读取时无效,则问题在于tty只向程序发送256个字符。这与stdin无关。问题在于tty,而tty与stdin不同。请停止将两者混为一谈。感谢alk,William Pursell,这是tty的问题,是吗当我重定向输入时,“方法2”正在修改指向缓冲区的指针。最好是索引到缓冲区或声明第二个指针。
#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;
}
#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 '\0'. */
    while ((p < buffer + sizeof buffer - 1) && ((c = getchar()) != EOF)) {
        if (c == '\n') { /* A NEWLINE, act on buffer, and skip it. */
             *p = '\0'; /* end the string */
             printf("Read %d chars: %s\n", p - buffer, buffer);
             /* crypt it ... */
             /* ... */
             p = buffer; /* reset buffer */
             continue;
        }
        *p++ = c; /* add the character to the buffer */
    }
    /* here you got EOF */
    return 0;
}