GVKun编程网logo

在检查 available() 之后调用 read() 时,stdin 不会在 Windows 上回显可中断 readline 实现

3

最近很多小伙伴都在问在检查available()之后调用read()时,stdin不会在Windows上回显可中断readline实现这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓

最近很多小伙伴都在问在检查 available() 之后调用 read() 时,stdin 不会在 Windows 上回显可中断 readline 实现这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展@available 和 #available、BUG!!!======> 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory、c 在 while(1) 循环中使用 stdin 如果 stdin 超过缓冲区大小将循环多次、chrome://inspect UI 在检查 Android 模拟器时损坏等相关知识,下面开始了哦!

本文目录一览:

在检查 available() 之后调用 read() 时,stdin 不会在 Windows 上回显可中断 readline 实现

在检查 available() 之后调用 read() 时,stdin 不会在 Windows 上回显可中断 readline 实现

如何解决在检查 available() 之后调用 read() 时,stdin 不会在 Windows 上回显可中断 readline 实现

我正在尝试在 Java 中实现一个可中断的 readline 方法 [1],因为标准例程(使用 system.in 周围的缓冲读取器)不是。我想出了一个在 Linux 上运行良好的例程,但是当您在 Windows 环境中运行它时,在用户按下 Enter 之前,按键不会回显到控制台 - 呸!

经过一些试验,似乎只有在用户键入时对 system.in.read() 进行阻塞调用时,按键才会被回显。

有谁知道这是否可以修复?我试图反映我使用私有 java.ui.Console#echo 方法的方式,但它似乎没有做任何事情。我的下一个目标是查看 JNA API,看看我是否可以从那里直接访问控制台。

1 - 作为参考,这是我的可中断 readline 实现。我捕获 ctrl-c 并中断调用 readline 的线程 - 在其他地方处理,工作,并且可能与此示例无关

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

public class EchoTest {

  private static final long MILLIS_BETWEEN_STDIN_CHECKS = 10;

  public static void main(String[] args) throws IOException,InterruptedException {
    System.out.println("Type something");
    // on windows,there''s no echo until enter,dammit!
    String input = readline(system.in);
    System.out.println("Hello," + input);
  }


  /**
   * Read a line of input from an input stream,presumably stdin.
   *
   * @param stdin An InputStream to read a line from. 
   *
   * @return the string that was read,with the line terminator removed.
   *
   * @throws IOException if there was an io error reading from stdin
   * @throws InterruptedException if the thread was interrupted while waiting to receive keypresses
   */
  public static String readline(InputStream stdin) throws IOException,InterruptedException {

    // we use mark to peek ahead for \\r\\n
    if (!stdin.markSupported()) {
      throw new IllegalArgumentException("stdin must support mark");
    }

    // result is stored in here
    StringBuffer stringBuffer = new StringBuffer(512);
    // a byte buffer as we read a byte at a time from stdin
    byte[] bytes = new byte[64];
    // number of bytes we''ve read in to the buffer
    int bytesRead = 0;

    while (true) {

      // check whether read should block
      if (stdin.available() > 0) {

        int byteRead = stdin.read();

        if (byteRead == -1) {
          // end of stream - not expected for readline
          throw new EOFException();
        }

        // check for line ending character sequences,we need to detect \\r,\\n or \\r\\n
        if (byteRead == ''\\n'') {
          // we''re done!
          break;
        } else if (byteRead == ''\\r'') {
          // we''re done,but we might need to consume a trailing \\n
          if (stdin.available() == 0) {
            // nothing is ready,we presume that if \\r\\n was sent,then they''d be sent as one and the buffer would
            // already have the \\n
            // worst case,the next call to readline will exit with an empty string - if this appears to happen,we
            // Could detect a \\n being in stdin at the start and drop it on the floor
            break;
          } else {
            // there is a byte there - mark our position and check if it''s \\n
            stdin.mark(1);
            if (stdin.read() == ''\\n'') {
              // it is we''re done
              break;
            } else {
              // it isn''t \\n,reset position for the next call to readline or read
              stdin.reset();
              break;
            }
          }
        } else {
          bytes[bytesRead++] = (byte)byteRead;

          // flush buffer if it''s full
          if (bytesRead == bytes.length) {
            stringBuffer.append(new String(bytes,bytesRead));
            bytesRead = 0;
          }

        }

      } else {
        if (Thread.interrupted()) {
          throw new InterruptedException();
        } else {
          Thread.sleep(MILLIS_BETWEEN_STDIN_CHECKS);
        }
      }

    }

    stringBuffer.append(new String(bytes,bytesRead));

    return stringBuffer.toString();
  }


}

@available 和 #available

@available 和 #available

Swift 2.0 中,引入了可用性的概念。对于函数,类,协议等,可以使用@available声明这些类型的生命周期依赖于特定的平台和操作系统版本。而#available用在判断语句中(if,guard,while等),在不同的平台上做不同的逻辑。

@available

用法

@available放在函数(方法),类或者协议前面。表明这些类型适用的平台和操作系统。看下面一个例子:

@available(iOS 9,*)
func myMethod() {
    // do something
}

@available(iOS 9,*)必须包含至少2个特性参数,其中iOS 9表示必须在 iOS 9 版本以上才可用。如果你部署的平台包括 iOS 8,在调用此方法后,编译器会报错。

另外一个特性参数:星号(*),表示包含了所有平台,目前有以下几个平台:

  • iOS
  • iOSApplicationExtension
  • OSX
  • OSXApplicationExtension
  • watchOS
  • watchOSApplicationExtension
  • tvOS
  • tvOSApplicationExtension

一般来讲,如果没有特殊的情况,都使用*表示全平台。

ottom:0.75em; font-size:16px; line-height:1.7em; text-indent:1em; color:rgb(51,*)是一种简写形式。全写形式是@available(iOS,introduced=9.0)introduced=9.0参数表示指定平台(iOS)从 9.0 开始引入该声明。为什么可以采用简写形式呢?当只有introduced这样一种参数时,就可以简写成以上简写形式。同理:@available(iOS 8.0,OSX 10.10,*) 这样也是可以的。表示同时在多个平台上(iOS 8.0 及其以上;OSX 10.10及其以上)的可用性。

另外,@available还有其他一些参数可以使用,分别是:

  • deprecated=版本号:从指定平台某个版本开始过期该声明
  • obsoleted=版本号:从指定平台某个版本开始废弃(注意弃用的区别,deprecated是还可以继续使用,只不过是不推荐了,obsoleted是调用就会编译错误)该声明
  • message=信息内容:给出一些附加信息
  • unavailable:指定平台上是无效的
  • renamed=新名字:重命名声明

以上参数具体可以参考官方文档

#available

#available用在条件语句代码块中,判断不同的平台下,做不同的逻辑处理,比如:

if #available(iOS 8,*) {
        // iOS 8 及其以上系统运行
}


guard else {
    return //iOS 8 以下系统就直接返回
}

stackoverflow 相关问题整理

  • Difference between @available and #available in swift 2.0: @available 和 #available

    帖子里面还提到一个问题:@available是编译期间判断的吗?而#available是运行时行为吗?

BUG!!!======> 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory

BUG!!!======> 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory

总结

以上是小编为你收集整理的 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory">BUG!!!======> 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

c 在 while(1) 循环中使用 stdin 如果 stdin 超过缓冲区大小将循环多次

c 在 while(1) 循环中使用 stdin 如果 stdin 超过缓冲区大小将循环多次

如何解决c 在 while(1) 循环中使用 stdin 如果 stdin 超过缓冲区大小将循环多次

我正在构建一个比我将展示的代码块更大的应用程序。但我基本上会要求用户输入(如转弯)执行任务,然后要求更多用户输入。我遇到的问题最好通过我的代码示例来展示。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(){
  4. while(1){
  5. char *string = malloc(6 * sizeof(char));
  6. printf("enter string: ");
  7. for(int i = 0; i < 6; i++){
  8. char input = getchar();
  9. if(input == ''\\n''){
  10. break;
  11. }
  12. string[i] = input;
  13. }
  14. for(int i = 0; i < 6; i++){
  15. printf("%d ",string[i]);
  16. }
  17. }
  18. }

当我输入一个 0 - 5 个字符的字符串时,结果完全符合预期,例如 0 0 0 0 0 0 enter string: 无数据或输入“abc”时97 98 99 0 0 0 enter string:

然而,当输入超过 5 个字符时,结果并不如预期,例如 97 98 99 100 101 102 enter string: 103 0 0 0 0 0 enter string: 当输入“abcdefg”时。出于某种原因,它似乎循环了两次,这可以扩展,例如13 ~2.1 倍大于 6 将循环 3 次,直到缓冲区为空。

这是为什么,如果有人输入 6 个或更多字符,我如何阻止它多次循环。

每次设置/清除 *string 时都会调用此循环中的副问题 malloc。这是创建一个新指针,我需要在每个循环结束时清除内存,还是覆盖 *string 的现有位置?

谢谢!

解决方法

以下将实现您想要的:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. while(1)
  6. {
  7. char string[6],input;
  8. printf("\\nEnter string: ");
  9. for(int i = 0; i < 6; i++)
  10. {
  11. input = getchar();
  12. if(input == ''\\n'')
  13. {
  14. break;
  15. }
  16. string[i] = input;
  17. }
  18. while (input != ''\\n'') input = getchar();//eats away remaining characters
  19. for(int i = 0; i < 6; i++)
  20. {
  21. printf("%d ",string[i]);
  22. string[i] = 0;//clears the array for next iteration
  23. }
  24. }
  25. }
,

虽然不要求在数组末尾包含空终止字符,但请理解,如果不这样做,则数组不能被视为字符串——这使得您可以选择变量名 { {1}} 具有误导性。

我们认为您确实只想读取 string 个字符,并且不想通过包含 nul-termination0 - 6 成为字符串/em> 字符作为输入的结尾。鉴于只涉及六个字符,因此不需要动态分配,实际上声明指针这样做,比六字节数组占用更多的内存(暂时保留编译器潜在的最小数组大小)

如果你确实需要分配和复制,你可以简单地添加,但现在,让我们看看修改你的循环,不管用户输入多少个字符,最多只能读取六个字符。简单的解决方案读取到 string''\\n'' 但最多只能存储六个字符,例如

  1. EOF

示例使用/输出

现在无论输入多少个字符,最多只能存储六个,其余的将被读取并静默丢弃,清空#include <stdio.h> #define MAXC 6 /* if you need a constant,#define one (or more) */ int main (void) { while (1) { char string[MAXC] = ""; /* buffer to hold 6 chars (NOT a string) */ int n = 0,c = 0; /* counter and temp character */ fputs ("enter string: ",stdout); /* prompt */ while ((c = getchar()) != ''\\n'' && c != EOF) { /* read char,test \\n & EOF */ if (n < MAXC) /* array not full */ string[n++] = c; /* store char,advance counter */ } if (!n) /* \\n alone,break */ break; for (int i = 0; i < n; i++) /* output result */ printf (i ? " %d" : "%d",string[i]); putchar (''\\n''); } } ,例如

  1. stdin

告诉我这是否是您所追求的,以及您是否需要分配和验证方面的帮助。如果您有任何其他问题,请在下方发表评论。

chrome://inspect UI 在检查 Android 模拟器时损坏

chrome://inspect UI 在检查 Android 模拟器时损坏

如何解决chrome://inspect UI 在检查 Android 模拟器时损坏

enter image description here

几天前它运行良好,但是现在我明白了。什么都不显示。用户界面看起来很奇怪

解决方法

做这三件事,如果它们不起作用,请评论这个答案。

-重启 Chrome
-重启你的电脑
- 卸载 Chrome 并重新安装

Sry 偏离主题,但我推荐 Firefox DEV(但这只是我的意见)

关于在检查 available() 之后调用 read() 时,stdin 不会在 Windows 上回显可中断 readline 实现的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于@available 和 #available、BUG!!!======> 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory、c 在 while(1) 循环中使用 stdin 如果 stdin 超过缓冲区大小将循环多次、chrome://inspect UI 在检查 Android 模拟器时损坏等相关内容,可以在本站寻找。

本文标签: