替换为cpp中基于getch的代码块

我偶然在Windows中用Turbo C ++ IDE编写了2016年的代码 那是接受密码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.placeholder">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="${launcherName}"
        android:supportsrtl="true"
        android:theme="@style/Theme.TwaSplash">

        <meta-data
            android:name="asset_statements"
            android:value="${assetStatements}" />

        <activity android:name="android.support.customtabs.trusted.Launcheractivity"
            android:label="${launcherName}">
            <meta-data android:name="android.support.customtabs.trusted.DEFAULT_URL"
                android:value="${defaultUrl}" />

            <meta-data
                android:name="android.support.customtabs.trusted.STATUS_BAR_COLOR"
                android:resource="@color/colorPrimary" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="https"
                    android:host="${hostName}"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

如果没有 linux getch方法来显示类似********的输出,是否可以代替它?我尝试了scanf,但是那没有用,因为它首先接受了整个输入,后来又给出了输出

iCMS 回答:替换为cpp中基于getch的代码块

C ++中没有标准替代品。

在Linux(以及类似的系统,例如BSD)上,有一个不建议使用的旧版功能getpass。我之所以提及它,是因为glibc中该函数的documentation建议如下:

这套精确的操作可能并不适合所有可能的情况。在这种情况下,建议用户编写自己的getpass替代项。例如,一个非常简单的替代方法如下:

#include <termios.h>
#include <stdio.h>

ssize_t
my_getpass (char **lineptr,size_t *n,FILE *stream)
{
  struct termios old,new;
  int nread;

  /* Turn echoing off and fail if we can’t. */
  if (tcgetattr (fileno (stream),&old) != 0)
    return -1;
  new = old;
  new.c_lflag &= ~ECHO;
  if (tcsetattr (fileno (stream),TCSAFLUSH,&new) != 0)
    return -1;

  /* Read the passphrase */
  nread = getline (lineptr,n,stream);

  /* Restore terminal. */
  (void) tcsetattr (fileno (stream),&old);

  return nread;
}

请注意,该示例是用C编写的,并且不是有效的C ++。需要进行一些小的更改,尤其是将C ++关键字new用作变量名。只需将其重命名为其他名称即可。

还要注意,行为是根本不回显,而不是回显*。这在POSIX系统中是常规的,并且更安全,因为它不会显示密码短语的长度。

本文链接:https://www.f2er.com/1884996.html

大家都在问