如何在 C 中写入和读取命名管道?
How to write and read from a named pipe in C?
我有 2 个程序(write.c 和 read.c)。我想从标准输入连续写入命名管道,并在另一端从中读取(并写入标准输出)。我做了一些工作,但它工作不正常。另一端的程序以错误的顺序读取或读取特殊字符(所以它读取的比它需要的多?)。我还希望能够将命名管道输出与某个字符串进行比较。
无论如何,这是来自两个文件的代码:
write.c:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFSIZE 512
#define err(mess) { fprintf(stderr,"Error: %s.", mess); exit(1); }
void main()
{
int fd, n;
char buf[BUFFSIZE];
mkfifo("fifo_x", 0666);
if ( (fd = open("fifo_x", O_WRONLY)) < 0)
err("open")
while( (n = read(STDIN_FILENO, buf, BUFFSIZE) ) > 0) {
if ( write(fd, buf, strlen(buf)) != strlen(buf)) {
err("write");
}
}
close(fd);
}
read.c:
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 512
#define err(mess) { fprintf(stderr,"Error: %s.", mess); exit(1); }
void main()
{
int fd, n;
char buf[BUFFSIZE];
if ( (fd = open("fifo_x", O_RDONLY)) < 0)
err("open")
while( (n = read(fd, buf, BUFFSIZE) ) > 0) {
if ( write(STDOUT_FILENO, buf, n) != n) {
exit(1);
}
}
close(fd);
}
输入示例:
hello how are you
123
test
错误输出示例:
hello how are you
b123
o how are you
btest
how are you
b
再输入一个例子:
test
hi
并输出:
test
hi
t
读取修改的缓冲区不是有效的 c 字符串所以
write(fd, buf, strlen(buf)) != strlen(buf) // write.c
是未定义的行为。你应该做
write(fd, buf, n) != n
因为你用read()
读取了n个八位字节。
这很有趣,因为你这样做是为了 read.c 而不是为了 write.c
n
的类型必须 ssize_t
而不是 int
、man read.
main()
必须return一个int
Declare main prototype
我有 2 个程序(write.c 和 read.c)。我想从标准输入连续写入命名管道,并在另一端从中读取(并写入标准输出)。我做了一些工作,但它工作不正常。另一端的程序以错误的顺序读取或读取特殊字符(所以它读取的比它需要的多?)。我还希望能够将命名管道输出与某个字符串进行比较。
无论如何,这是来自两个文件的代码:
write.c:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFSIZE 512
#define err(mess) { fprintf(stderr,"Error: %s.", mess); exit(1); }
void main()
{
int fd, n;
char buf[BUFFSIZE];
mkfifo("fifo_x", 0666);
if ( (fd = open("fifo_x", O_WRONLY)) < 0)
err("open")
while( (n = read(STDIN_FILENO, buf, BUFFSIZE) ) > 0) {
if ( write(fd, buf, strlen(buf)) != strlen(buf)) {
err("write");
}
}
close(fd);
}
read.c:
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 512
#define err(mess) { fprintf(stderr,"Error: %s.", mess); exit(1); }
void main()
{
int fd, n;
char buf[BUFFSIZE];
if ( (fd = open("fifo_x", O_RDONLY)) < 0)
err("open")
while( (n = read(fd, buf, BUFFSIZE) ) > 0) {
if ( write(STDOUT_FILENO, buf, n) != n) {
exit(1);
}
}
close(fd);
}
输入示例:
hello how are you
123
test
错误输出示例:
hello how are you
b123
o how are you
btest
how are you
b
再输入一个例子:
test
hi
并输出:
test
hi
t
读取修改的缓冲区不是有效的 c 字符串所以
write(fd, buf, strlen(buf)) != strlen(buf) // write.c
是未定义的行为。你应该做
write(fd, buf, n) != n
因为你用read()
读取了n个八位字节。
这很有趣,因为你这样做是为了 read.c 而不是为了 write.c
n
的类型必须 ssize_t
而不是 int
、man read.
main()
必须return一个int
Declare main prototype