Tag: 终端

使用C在终端中打印多字节字符

我一直在试验一个自定义字符串对象(struct),如下所示: typedef struct { int encoding; int length; character * array; } EncodedString; 我的想法是,通过指定编码,我可以制作一些使用该编码的函数来正确打印字符串,即ASCII或utf-8或utf-16等。(请原谅我的字符编码无知。) 现在,我正打算打印出一个(普通话)汉字:狗(0x72d7)。 我想也许是通过逐个字符打印它,它会正常工作,但显然不是。 它打印只是“r?” (分别为0x72和0xd7)。 那么如何修改这个程序以便它打印出角色? #include typedef unsigned char character; typedef struct { int encoding; int length; character * array; } EncodedString; void printString(EncodedString str); int main(void) { character doginmandarin[] = {0x72U, 0xd7U}; EncodedString mystring = {0, sizeof doginmandarin, doginmandarin}; printString(mystring); […]

在终端中禁用本地回显的含义

我在Linux操作系统上运行了交流应用程序。 此应用程序从终端获取键盘密钥并将它们发送到远程服务器。 下面的代码打开终端: // save old terminal attributes if (tcgetattr(0, &ttyold) != 0) { fprintf(stderr, “Failed getting terminal attributes\n”); goto out; } ttynew = ttyold; ttynew.c_iflag = 0; ttynew.c_oflag = 0; // disable canonical mode (don’t buffer by line) ttynew.c_lflag &= ~ICANON; // disable local echo ttynew.c_lflag &= ~ECHO; ttynew.c_cc[VMIN] = 1; ttynew.c_cc[VTIME] = 1; […]

stdin是否包含回车符(\ r)char?

在SO答案中我写了这段代码: char fail_on_eof (int c) { if (c == EOF) exit (EXIT_FAILURE); return (char) c; } void skip_to_next_line (void) { char c; do { c = fail_on_eof (getchar ()); } while (c != ‘\n’); } 我想知道是否有任何终端可以返回’\r’ (只要在行尾有一个’\n’ ,上面的代码就会覆盖它)。 stdin是否包含’\r’ (在任何平台上)? 输入重定向怎么样stdin总是在文本模式下工作?

命令行参数未在C中执行

我试图将参数传递给ls命令的execl()函数。 但是当我通过时 /bin/ls -l -a 作为我的程序的参数,execl()函数不识别最后两个参数。 为什么?这是代码: #include #include int main(int argc, char *argv[]) { int i,childpid; if(argc!=4) { printf(“you didn’t provide any commandline arguments!\n”); return 0; } childpid=fork(); if(childpid0); printf(“My ID %d, Parent ID %d, CHild ID %d\n”, getpid(),getppid(),childpid); return 0; } 我在Ubuntu上。 问候

哈佛CS50库,需要帮助在Mac OS X上安装

我正在从cs50.tv学习这门课程,这是哈佛扩展学校,在课程中他们使用的是他们制作的名为cs50的图书馆,在这个链接上可以下载 https://manual.cs50.net/CS50_Library#Mac_OS 我下载了zip文件并将其解压缩,然后打开终端并以cd的方式进入库目录,但每次我都按照手册中的步骤操作。 我执行此命令后立即gcc -c -ggdb -std = c99 cs50.c -o cs50.o 我在终端中收到此错误 cs50.c:15:16: error: gc.h: No such file or directory cs50.c: In function ‘GetString’: cs50.c:207: warning: implicit declaration of function ‘GC_FREE’ cs50.c:212: warning: implicit declaration of function ‘GC_REALLOC’ cs50.c:212: warning: initialization makes pointer from integer without a cast cs50.c:230: warning: implicit declaration of function […]

如何更改termios配置,以便当用户按键时getc()立即返回?

我想为CLI应用程序实现自动完成function。 只有在输入以下字符列表时,才会返回getc()的默认行为:NEW_LINE或EOF。 我想将TAB添加到此列表中,以便我可以触发自动完成算法。 有没有办法做到这一点,例如,使用termios? editline库(http://www.thrysoee.dk/editline/)可以做到,但我无法想象它是怎么做的?

将非ASCII字符写入tty上的程序员的stdin(通过ssh)

我被SSH连接到一个带有一些二进制挑战的远程服务器。 有一次它要求我输入文字。 我知道它正在使用fgets()来读取stdin ,并且我可以溢出它被复制到的地方并覆盖附近的变量。 问题是我不知道如何键入我需要的地址值, \x84和\x04等。如果我能够使用bash我会echo -ne “\x84″或使用Chex数组但我可以’在这里做那种事。 我尝试使用hex到ASCII转换器并复制二进制字符,并使用expect脚本发送二进制值,但两者都有相同的问题。 x84添加了额外的字符,x04根本不会写入。 任何想法是可靠地将值写入内存的最佳方法,这些内容在Unix tty上通过ssh无法用ASCII字符表示?

C:主要没找到,但它就在那里 编译错误

当我编译下面给出的两个.c文件时,我得到一个非常奇怪的错误。 终端编译代码 gcc -I. -o main.c matrix.c -lblas -lgfortran 错误: /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11 /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12 /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2 /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2 /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11 […]

自定义shell只接受一个参数

所以我有一个shell的代码: #include #include #define MAX_NUM_ARGS 256 #define SIZE 256 void orders(char *command[SIZE]); int main() { char buffer[SIZE]= “”; //char input_args[MAX_NUM_ARGS]; char **input_args = NULL; int i = 0;// counting variable int blah = 0; printf(“Welcome to the AY shell.\n”); while(1){ //initialize array of strings //first free any prevously allocated memory if (input_args != NULL) { […]

C / C ++:按ENTER键后,我可以将光标保持在当前行吗?

我想问一下按ENTER后是否有办法将光标保持在当前行! 例如 … #include int main() { int d=0; printf(“Enter a number : “); scanf(“%d”,&d); if(d%2)printf(” is a Odd number\n”); else printf(” is a Even number\n”); return 0; } 输出的一个例子: Enter a number : 10 is a Even number ……但我需要的是这样的: Enter a number : 10 is a Even number 我想在用户输入的数字旁边加上“是偶数”(或“是奇数”)