在SCO Unix OS中获取用户输入时遇到的问题

时间:2017-12-28 14:11:38

标签: c unix cc sco-unix

在尝试通过我的代码获取用户输入时,我遇到了一个奇怪的问题。我很确定问题不在于代码,而是与标准输入流(stdin)或类似的操作系统有关,但由于我没有其他具有类似操作系统设置的机器(因为它& #39;现在几乎不可能找到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 .....

我已尝试但未成功的其他方法包括更改保存值的缓冲区的数据类型(从字符串到unsigned long),动态分配内存到缓冲区,设置{{1}如无缓冲等

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

2 个答案:

答案 0 :(得分:1)

请参阅SCO网站上的ioctl()和stty()手册页。您应该能够通过测试终端与重定向来检索设置中的差异。

答案 1 :(得分:0)

好吧,您的程序(都)有错误:

/* 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);
}

第二个更糟:

/* unbalanced parenthesis, you lack a parenthesis after 'while' keyword */
while(ch = getc(stdin)) != EOF)
{

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

/* 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: %s\n", 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 '\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;
}

最后的笔记:

不要发布代码段,而是完整的示例,因为很难确定哪些错误是在此处复制代码时的错误,或者是您在原始程序中所犯的错误。