我想制作交互式控制台应用程序,它允许循环输入命令。例如,用户输入“搜索”,程序会找到一些数据并将其打印在屏幕上。然后程序等待下一个命令(可以是搜索、退出或其他)。为了用户的方便,我希望我的程序支持命令历史记录(就像在终端中,当按键盘上的向上和向下箭头时)。但我无法意识到如何做到这一点,因为我不知道如何打印可以通过 scanf、std::getline、std::cin 等进一步读取的文本。\n所以代码std::cin << "hello";不是\n已编译(与 \xe2\x80\x98std::cin << "hello"\xe2\x80\x99 中的 \xe2\x80\x98operator<<\xe2\x80\x99 不匹配)。函数fprintf(stdin, "hello");不打印任何内容,并且 scanf 无法读取此打印消息。显然std::getline(std::cin, str);, andscanf("%s", s);等gets(s)无法读取printfor输出的文本std::out。所以问题是:如何在控制台上打印stdin( std::cin) 中的文本?或者也许有更优雅的方式来组织命令历史记录?
PS我还考虑过模拟按下按钮来打印我需要的文本,但我希望有更好的方法来创建命令历史记录
\n\nPPS 我使用 Linux 和 C++
\n我正在阅读以下指南http://www.postgresonline.com/journal/archives/329-An-almost-idiots-guide-to-install-PostgreSQL-9.3,-PostGIS-2.1-and-pgRouting-with-Yum .html(此链接来自http://postgis.net/install).该命令yum install postgis2_93给出以下错误:
Error: Package: gdal-libs-1.9.2-6.rhel6.x86_64 (pgdg93)
Requires: libhdf5.so.6()(64bit)
Error: Package: gdal-libs-1.9.2-6.rhel6.x86_64 (pgdg93)
Requires: libdapserver.so.7()(64bit)
Error: Package: gdal-libs-1.9.2-6.rhel6.x86_64 (pgdg93)
Requires: libCharLS.so.1()(64bit)
Error: Package: postgis2_93-2.1.3-1.rhel6.x86_64 (pgdg93)
Requires: libjson.so.0()(64bit)
Error: Package: gdal-libs-1.9.2-6.rhel6.x86_64 (pgdg93)
Requires: libgta.so.0()(64bit)
Error: Package: gdal-libs-1.9.2-6.rhel6.x86_64 (pgdg93)
Requires: libspatialite.so.2()(64bit)
Error: Package: gdal-libs-1.9.2-6.rhel6.x86_64 (pgdg93)
Requires: libcfitsio.so.0()(64bit)
Error: Package: postgis2_93-2.1.3-1.rhel6.x86_64 (pgdg93)
Requires: json-c
Error: Package: postgis2_93-2.1.3-1.rhel6.x86_64 (pgdg93)
Requires: hdf5
Error: Package: armadillo-4.300.0-1.rhel6.x86_64 (pgdg93)
Requires: libhdf5.so.6()(64bit)
Error: Package: gdal-libs-1.9.2-6.rhel6.x86_64 (pgdg93)
Requires: libnetcdf.so.6()(64bit)
Error: Package: gdal-libs-1.9.2-6.rhel6.x86_64 (pgdg93) …Run Code Online (Sandbox Code Playgroud) 我param1=value1¶m2=¶m3=value3使用urllib.parse.parse_qs(python 3 wsgi应用程序)解析以下请求,但此函数返回仅带param1和param3键的dict .我可以使用什么功能来获得空param2?
我已经阅读了许多有关此警告的问题(取消引用类型双关指针会破坏严格别名规则,取消引用类型双关指针会破坏严格别名规则 [-Wstrict-aliasing],严格别名规则是什么?,“取消引用类型双关指针将破坏严格的别名规则”警告和其他人)并且对我的警告完全感到困惑。
所以我有一个结构:
typedef struct {
unsigned char precision;
unsigned char scale;
unsigned char array[33];
} DBNUMERIC;
Run Code Online (Sandbox Code Playgroud)
当从 MS SQL Server 检索数据时,该结构由 FreeTDS 库填充。我知道从array[1]那里开始是 64 位整数(大端),我想得到它。我使用以下代码:
int64_t result = 0;
result = be64toh(*((decltype(result)*)(numeric.array + 1)));
Run Code Online (Sandbox Code Playgroud)
但是 GCC 给了我警告dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]。但是如果我使用代码:
int64_t result = 0;
decltype(result)* temp_ptr = (decltype(result)*)(numeric.array + 1);
decltype(result) temp = *temp_ptr;
result = be64toh(temp);
Run Code Online (Sandbox Code Playgroud)
没有关于违反严格别名规则的警告。我不认为此代码与原始代码不同,因此我很困惑。如何将数组中的 8 个字节转换为int64_t变量?
如果找不到特定文件,我希望我的脚本结束.因此我写了以下内容:
[ -f "$DAEMON" ] || (echo "File $DAEMON not found" && exit 0)
Run Code Online (Sandbox Code Playgroud)
但它不能正常工作.如果该文件不存在,我的脚本将输出此错误消息并继续工作.
我试过了
[ -f "$DAEMON" ] || { echo "File $DAEMON not found" && exit 0 }
Run Code Online (Sandbox Code Playgroud)
(因为这里建议退出在bash中不起作用),但是我得到语法错误(意外的文件结束).
[ -f "$DAEMON" ] || exit 0效果很好,但[ -f "$DAEMON" ] || (exit 0)
没有.
如何输出错误消息并停止执行脚本?
我的意思是以下.我有几个继承相同基类的类.Union包含这些类的指针:
#include "stdio.h"
class A {
public:
A() { printf("A\n"); }
virtual ~A() { printf("~A\n"); }
};
class B : public A {
public:
B() { printf("B\n"); }
virtual ~B() { printf("~B\n"); }
};
class C : public A {
public:
C() { printf("C\n"); }
virtual ~C() { printf("~C\n"); }
};
int main() {
union {
B* b;
C* c;
} choice;
choice.b = new B();
delete choice.c; //We have B object, but deleting C
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它似乎有效,但我不确定它是否不是特定于实现的行为.我可以使用这种奇怪的删除方法,还是应该记住一种存储对象并分别删除它?
PS我使用C++ …
c++ ×3
linux ×3
bash ×1
c++11 ×1
console ×1
gcc ×1
gcc-warning ×1
postgis ×1
postgresql ×1
python ×1
python-3.2 ×1
python-3.x ×1
rhel ×1
sh ×1
terminal ×1
urllib ×1
wsgi ×1