Tag: 命令

使用execvp()运行’wc’识别/home/usr/foo.txt但不识别〜/ foo.txt

我有一个程序,我想使用execvp()运行命令wc 。 重要的部分在这里: char *argvNew[5]; argvNew[0] = “wc”; argvNew[1] = “/home/user/foo.txt”; argvNew[2] = NULL; execvp(argvNew[0], arvgNew); 这很好用,终端显示wc的输出。 但是,当我将行更改为: argvNew[1] = “~/foo.txt” 我在终端中收到此错误: wc: ‘~/foo.txt’: No such file or directory 直接从终端运行wc ~/foo.txt的行为与运行wc /home/user/foo.txt完全相同。 为什么execvp与~/ part存在问题?

伴随其他值时的EOF行为

*注意:我正在使用Windows,所以EOF对我来说是ctrl + Z. 有一段时间我注意到,EOF输入在隔离方面的行为似乎与其他输入时相同。 例如, ^Z (命令提示符中的Windows的EOF命令)和a^Z似乎在以下代码中导致不同的行为: #include #define MAX 1000 int getline(char s[]); main() { int line; char arr[MAX]; while( (line = getline(arr)) != EOF) printf(“%s”,arr); system(“Pause”); return 0; } int getline(char s[]) { int c, i = 0; while ((c = getchar()) != EOF && c != ‘\n’) { s[i++] = c; } […]

C程序对三个命令执行管道

我必须编写一个程序来执行与du |相同的操作 排序| 在命令行的头会做,但我卡住了,我的程序不工作。 现在的输出是112。 并且该程序不会终止。 请帮忙,我不知道该怎么办! int main(void) { int fd[2]; int fd1[2]; int pid; if (pipe(fd) == -1) { perror(“Pipe”); exit(1); } switch (fork()) { case -1: perror(“Fork”); exit(2); case 0: dup2(fd[1], STDOUT_FILENO); close(fd[0]); close(fd[1]); execl(“/usr/bin/du”, “du”, (char *) 0); exit(3); } if (pipe(fd1) == -1) { perror(“Pipe”); exit(1); } switch (fork()) { […]

如何在ps -e中显示proccess

你好! 我想制作简单的c程序,它会像ps -e一样工作。 应该显示的唯一列是PID和CMD。 那是我的代码: #include #include #include #include #include int main() { DIR *dir; struct dirent *entry; if ((dir = opendir(“/proc”)) == NULL) perror(“operation error”); else { printf(“PID CMD\n”); while ((entry = readdir(dir)) != NULL) printf(” %s\n”, entry->d_name); closedir(dir); } return 0; } 我的任务是: 1)我如何只显示带数字的文件夹(我不知道如何实现regcomp())? 2)如何靠近PID写入CMD(如果是带数字的文件夹,我不能用路径粘贴(?)字符串)?

是否有办法警告行为不端的指定初始化器?

C99引入了结构的指定初始化器的概念。 例如,给定: typedef struct { int c; char a; float b; } X; 我可以初始化为: X foo = {.a = ‘\1’, .b = 2.0F, .c = 4}; 并且调用: printf(“c = %d\na = %hhu\nb = %f”, foo.c, foo.a, foo.b); 输出: c = 4 a = 1 b = 2.000000 如此处所述,这具有分配给c然后b然后b的“令人惊讶的行为”,与我指定的初始化器的顺序无关。 如果我有这样的函数,这将成为一个真正的问题: int i = 0; int f() […]

位字节式如何影响C中的按位移位和文件IO?

让L和B成为两台机器。 L从LSB(最低有效位)到MSB(最高有效位)对其位进行排序,而B从MSB到LSB进行排序。 或者,换句话说, L使用Little Endian而B使用Big Endian 位 – 不要与字节排序混淆。 问题1已解决 : 我们正在编写以下希望可移植的代码: #include int main() { unsigned char a = 1; a <<= 1; printf("a = %d\n", (int) a); return 0; } 在L上 ,它会打印2,但B上会发生什么? 它会将1移出并打印0吗? 解决方案: 6.5.7中的C99定义表明,至少在无符号整数类型上, <<和>>将分别乘以并除以2。 问题2: 我们正在编写以下希望可移植的代码: 阅读程序: /* program READ */ #include int main() { FILE* fp; unsigned char a; fp […]

在C ++中执行线程的奇怪顺序

我试图从Linux Tutorial Posix Threads执行第一个例子。 这就是我所拥有的: [alex@Allok c_c++]$ g++ -lpthread from.cpp from.cpp: In function ‘int main()’: from.cpp:10:22: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] from.cpp:11:22: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] [alex@Allok c_c++]$ ./a.out Thread 2 Thread 1 Thread 1 returns: 0 Thread 2 returns: 0 问题是我希望输出像源一样说: Thread 1 Thread 2 […]

如何使用C函数执行Shell内置命令?

我想通过像execv()这样的C语言函数执行Linux命令“pwd”。 问题是没有一个名为“pwd”的可执行文件,我无法执行“echo $ PWD”,因为echo也是一个没有可执行文件的内置命令。