如果我想通过管道将数据字节输入 Linux 上的 C/C++ 程序,如下所示:
cat my_file | ./my_app
但:
从 C/C++ 应用程序读取管道的最快技术是什么?
我做了一点研究,发现:
read()std::cin.read()popen() 但我不确定是否有更好的方法,或者以上哪种方法更好。
编辑:对此有性能要求,因此我要求使用开销最小的技术。
我有一个单行命令,让我们说
grep needle haystack.file
Run Code Online (Sandbox Code Playgroud)
如果我想使用 pwd 命令用当前工作目录替换“needle”怎么办。现在可能可以使用管道,但我需要针部分来显示工作目录和命令的其余部分是相同的。
所以最好是这样的:
grep (pwd) haystack.file
Run Code Online (Sandbox Code Playgroud)
执行时实际上会运行以下命令:
grep /var/www/html/ haystack.file
Run Code Online (Sandbox Code Playgroud)
我做了一些搜索,发现了很多带有管道的例子,但它不能应用于我的场景,因为第一部分 (grep) 和第二部分 (haystack.file) 在应用程序中是固定的。
我对 Angular 还很陌生,我正在尝试制作一个管道来从我的表格中过滤项目,但我不太确定如何去做。我试图只显示 EmpKey = empInfo[selectedEmployee].EmpKey 的表字段。任何帮助将不胜感激!提前致谢!
这是我的 tracker.component.html
<div class="row">
<div [ngClass]="{'col-xs-12':isHidden === true, 'col-xs-7': isHidden !== false}">
<button class="form-control" style="width:150px;" (click)="toggleSummary()">Open Summary</button>
<select id="empName" [(ngModel)]="selectedEmployee">
<option selected="selected" disabled>Employee Name...</option>
<option *ngFor="let emp of empInfo; let i = index" [ngValue]="i">{{i}} - {{emp.EmpID}}</option>
</select>
<select id="PTOtype">
<option selected="selected" disabled>Type of PTO...</option>
<option value="PTO">PTO</option>
<option value="ETO-Earned">ETO - Earned</option>
<option value="ETO-Used">ETO - Used</option>
<option value="STDLTD">STD/LTD</option>
<option value="Uncharged">Uncharged</option>
</select>
<button class="form-control" style="width: 150px;" (click)="nextEmployee()">Next</button>
<button class="form-control" style="width: 150px;" (click)="previousEmployee()">Previous</button>
<h2 *ngIf="empInfo && empInfo.length …Run Code Online (Sandbox Code Playgroud)我真的不知道为什么当我尝试在文件描述符 fd2 上使用 write() 时,它只会结束程序。当我使用 valgrind 时,我得到“进程以信号 13 (SIGPIPE) 的默认操作终止”。有问题的行是write(fd2[1], &num, sizeof(num));
这段代码的重点是创建一个子进程,然后让子进程生成一个随机数,然后通过管道将其传递给父进程。然后父母需要创建另一个孩子并将他从第一个孩子收到的数字通过另一个管道传递给第二个孩子。这个程序是针对我的一门大学课程的。
/*
* Ejemplo de codigo que genera un numero aleatorio y lo muestra por pantalla
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int pid1,pid2,fd1[2],fd2[2],pipe_status1,pipe_status2;
pipe_status1=pipe(fd1);
if(pipe_status1 == -1) {
perror("Error creando la tuberia 1\n");
exit(EXIT_FAILURE);
}
pipe_status2=pipe(fd2);
if(pipe_status2 == -1) {
perror("Error creando la tuberia 2\n");
exit(EXIT_FAILURE);
}
pid1 = fork();
if (pid1 < …Run Code Online (Sandbox Code Playgroud) 我一直在尝试做的是建立一个分叉两个孩子的程序。父进程从 stdin 读取,然后通过管道将其重定向到其子进程。然后孩子复制管道的读取端,以便它可以从标准输入(而不是管道)读取,然后将其打印到标准输出。这是代码:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char **argv) {
char buf[100];
int pipe_one[2];
// int pipe_two[2];
pipe(pipe_one);
// pipe(pipe_two);
// First child
switch(fork()) {
case -1:
exit(EXIT_FAILURE);
case 0:
close(pipe_one[1]);
dup2(pipe_one[0], STDIN_FILENO);
close(pipe_one[0]);
while (fgets(buf, 100, stdin)) {
fputs(buf, stdout);
}
fflush(stdout);
exit(EXIT_SUCCESS);
default:
break;
}
// Second child
/* switch(fork()) {
case -1:
exit(EXIT_FAILURE);
case 0:
close(pipe_two[1]);
dup2(pipe_two[0], STDIN_FILENO);
close(pipe_two[0]);
while (fgets(buf, 100, stdin)) {
fputs(buf, stdout);
}
fflush(stdout); …Run Code Online (Sandbox Code Playgroud) 我正在练习 bash。当我尝试此命令时,出现错误:“grep: where: No such file or directory”。
cat file2.txt | tr " " "\n" | grep –i where | wc -l
Run Code Online (Sandbox Code Playgroud)
Content of file2.txt = 1 2 3 4 where 5 where 7 where
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 NamedPipe 在 Win10 中的应用程序和服务之间进行通信。APP是用C#(UWP)开发的,前台作为Pipe Client运行。服务是 C++ 运行后台作为管道服务器。现在的问题是APP无法连接服务。我知道 MSFT 文档说管道仅在应用程序容器中受支持。但我尝试了以下情况:我的 uwp 应用程序 VS C#(nonUWP) 服务器(不在应用程序容器中);C++ 客户端 VS C++ 服务器(与服务相同的代码,除了在前台运行)。两种情况都可以正常工作。所以我认为它的安全权限可能有问题。但是我找不到任何异常,有人可以帮助我吗?
客户端(UWP/C#):
_namedPipeClientHandle[index] = CreateFileW(@"\\.\pipe\starpipe",
DESIREDACCESS.GENERIC_READ | DESIREDACCESS.GENERIC_WRITE,
SHAREMODE.FILE_SHARE_READ | SHAREMODE.FILE_SHARE_WRITE,
0,
CREATIONDISPOSITION.OPEN_EXISTING,
FLAGSANDATTRIBUTES.FILE_FLAG_OVERLAPPED,
0);
if (_namedPipeClientHandle[index] != null && _namedPipeClientHandle[index].IsInvalid == false)
{
_namedPipeClientStream[index] = new FileStream(_namedPipeClientHandle[index], FileAccess.ReadWrite, 2048, true);
isConnected[index] = true;
break;
}
Run Code Online (Sandbox Code Playgroud)
服务器(C++):
EXPLICIT_ACCESS ea[2];
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
SECURITY_ATTRIBUTES sa;
if (!AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID)) return …Run Code Online (Sandbox Code Playgroud) 我需要对程序的标准输出流做两件事:比如说,将它导入两个管道,或者将它打印到终端并将其导入管道。但是 - 这些东西都没有将它定向到文件中。
如果我想要一个管道 + 一个文件,我会使用tee命令:myprog | tee out.txt | another_command,如解释here。但是如果这两个操作都没有写入文件呢?
我在客户端和服务器之间有一个 TCP 多线程通信。我不知道为什么第三次使用 read 函数时它只给出 0。
while(1)
{
/* citirea raspunsului dat de server
(apel blocant pina cind serverul raspunde) */
if (read(sd, msg, 1024) < 0) {
perror("[client]Eroare la read() de la server.\n");
return errno;
}
/* afisam mesajul primit */
printf("[client]Mesajul primit este: %s\n", msg);
if(strcmp(msg, "Conexiune incheiata") == 0)
break;
memset(mesaj,0,256);
cin>>mesaj;
/* trimiterea mesajului la server */
if (write(sd, mesaj, 1024) <= 0) {
perror("[client]Eroare la write() spre server.\n");
return errno;
}
}
/* inchidem …Run Code Online (Sandbox Code Playgroud) 好的。我正在尝试获取一些随机数据并将其翻倍几次。所以我:
sridhar@ip-10-118-18-248:/scratch$ for each in 1 2 3 4 5 6 7 8 9 10 ; do pv randomtext | cat >> randomtext ; cp randomtext "randomtext-${each}" ; ls -l randomtext* ; done
3.45KiB 0:00:00 [99.2MiB/s] [================================>] 100%
-rw-rw-r-- 1 sridhar sridhar 7070 Aug 6 22:09 randomtext
-rw-rw-r-- 1 sridhar sridhar 7070 Aug 6 22:09 randomtext-1
6.90KiB 0:00:00 [ 269MiB/s] [================================>] 100%
-rw-rw-r-- 1 sridhar sridhar 14140 Aug 6 22:09 randomtext
-rw-rw-r-- 1 sridhar sridhar 7070 Aug 6 22:09 randomtext-1 …Run Code Online (Sandbox Code Playgroud)