2 개의 명명 된 파이프에서 데이터를 읽고 2 개의 입력에서 내용을 연결하는 다른 명명 된 파이프에 쓰려고합니다. 그러나 왜 내 출력은 첫 번째 입력의 문자열 만 표시합니까? 코드의 의심스러운 부분이문자열의 전체 내용을 표시하지 않는 명명 된 파이프에 쓰기
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define MAX_REC_SIZE 1024
int open_fifo(char *name, int mode) {
mode = mode == O_RDONLY ? (O_RDONLY | O_NONBLOCK): mode;
int fd;
if (access(name, F_OK) == -1) {
if(mkfifo(name, 0777) != 0) {
fprintf(stderr, "Could not create fifo %s\n", name);
exit(EXIT_FAILURE);
}
}
fd = open(name, mode);;
return fd;
}
void read_fifo(int fd, char *out_r) {
memset (out_r, '\0', MAX_REC_SIZE);
do {
if(read(fd, out_r, MAX_REC_SIZE) > 0) {
out_r = strtok(out_r, "\n");
return;
}
} while (1);
}
void write_fifo(int fd, char *out_w) {
write(fd, out_w, sizeof(out_w));
}
int main()
{
int pipe_fd[3], i;
char *pipe_nm[] = {"./in_pipe_1", "./in_pipe_2", "./out_pipe_1"};
int read_mode = O_RDONLY;
int write_mode = O_WRONLY;
char out[MAX_REC_SIZE];
char out_store[MAX_REC_SIZE];
for(i=0; i<3; i++) {
pipe_fd[i] = open_fifo(pipe_nm[i], i == 2 ? write_mode : read_mode);
}
read_fifo(pipe_fd[0], out);
strcpy(out_store, out);
read_fifo(pipe_fd[1], out);
strcat(out_store, out);
strcat(out_store, "\n");
write_fifo(pipe_fd[2], out_store);
return 0;
}
Offtopic : 많은 사람들이 깨끗한'memset (out_r, 0, MAX_REC_SIZE);'대신'memset (out_r, '\ 0', MAX_REC_SIZE);을 선호하는 이유를 이해하지 못합니다. 어쩌면 그들은 '\ 0'이 0과 다를 것이라고 생각할 것입니다. – i486
@ i486 :'char out [MAX_REC_SIZE] = {0}'을 선언 할 때 그는 그것을 초기화하는 것이 훨씬 낫습니다. –
@ i486 어떤 사람들은 값이 숫자가 아닌 문자로 해석된다는 것을 나타 내기 위해 문자 리터럴을 사용하는 것을 선호합니다. – fuz