我有一個程序需要兩個文件作爲參數。第一個文件將被複制到第二個文件中。該程序分爲兩個孩子,第一個孩子讀取文件並將其通過管道傳遞給另一個孩子,然後另一個孩子將其寫入文件。這兩個文件最終應該是完全相同的。diff cmd,二進制文件的x和y不同
當我運行DIFF兩個文件我碰到下面的錯誤比較:
[email protected]:~/Documents/OSprojects$ ./parent test.txt test2.txt
[email protected]:~/Documents/OSprojects$ cat test.txt
123456789112233445566778899
[email protected]:~/Documents/OSprojects$ cat test2.txt
123456789112233445566778899
[email protected]:~/Documents/OSprojects$ diff test.txt test2.txt
Binary files test.txt and test2.txt differ
[email protected]:~/Documents/OSprojects$
正如你可以看到他們都是一樣的,但DIFF打印出它們是不同的。顯然,它只是我不瞭解diff cmd。任何幫助,將不勝感激。
我相信由於某些原因,我創建的文件是一個二進制文件,而第一個文件不是,但我不知道爲什麼它是一個二進制文件。我認爲這可能與這行代碼來執行:
write(1, buf, BUF_SIZE); //write to buffer
memset(buf, '\0', BUF_SIZE);
在這個被寫出到緩衝區,然後我清除緩衝區中的孩子之一。我清除緩衝區是否錯誤?
這裏是貓-e的結果:
[email protected]:~/Documents/OSprojects$ cat -e test2.txt
123456789112233445566778899$
^@^@^@^@[email protected]:~/Documents/OSprojects$
這裏是CMP的結果:
[email protected]:~/Documents/OSprojects$ cmp test.txt test2.txt
cmp: EOF on test.txt
[email protected]:~/Documents/OSprojects$
我相信,是我的問題,我怎麼能清除該緩衝區,因此不會把這些扔到底?
所有我的代碼::作者
家長:
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUF_SIZE 16
void exitWithError(char* errorMsg, int exitWith); //generic error out function
void launch_writer(const char* pathname, char* const argv[], int pfd[]);
void launch_reader(const char* pathname, char* const argv[], int pfd[]);
int main(int argc, char* argv[]){
//making the pipe
int pfd[2];
if(pipe(pfd) == -1) //test pipe creation
exitWithError("PIPE FAILED", 1);
//forking
pid_t reader_child_pid;
pid_t writer_child_pid;
//args for each fork
char *args_1[] = {"reader", argv[1], (char *) 0};
char *args_2[] = {"writer", argv[2], (char *) 0};
if((writer_child_pid = fork()) == -1) {
exitWithError("WRITER FORK FAILED", 1);
}
else if (writer_child_pid == 0) { //first child comes here
launch_writer("./writer", args_2, pfd);
}
else if ((reader_child_pid = fork()) == -1) {
exitWithError("READER FORK FAILED", 1);
}
else if (reader_child_pid == 0) { //second child comes here
launch_reader("./reader", args_1, pfd);
}
//parent picks up here
//close off pipe from parents end
close(pfd[0]);
close(pfd[1]);
//wait for all processes to exit before ending
for(;;) {
if(wait(NULL) == -1){
if(errno == ECHILD)
exit(0);
else {
exitWithError("WAIT ERROR", 1);
}
}
}
}
void exitWithError(char* errorMsg, int exitWith) {
perror(errorMsg);
exit(exitWith);
}
void launch_writer(const char* pathname, char* const argv[], int pfd[]) {
dup2(pfd[0], 0);
close(pfd[1]);
close(pfd[0]);
execve(pathname, argv, NULL);
perror("execve failed");
}
void launch_reader(const char* pathname, char* const argv[], int pfd[]) {
dup2(pfd[1], 1);
close(pfd[1]);
close(pfd[0]);
execve(pathname, argv, NULL);
perror("execve failed");
}
兒童1:
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 16
int main(int argc, char* argv[]){
//Opens file to be read from
int inFile = open(argv[1], O_RDONLY);
//declaring variables
char buf[BUF_SIZE]; //temp hold whats read/written
int read_test; //check if EOF
for(;;) {
read_test = read(inFile, buf, BUF_SIZE); //read from file
if(read_test == 0) //eof
break;
write(1, buf, BUF_SIZE); //write to buffer
memset(buf, '\0', BUF_SIZE);
}
close(inFile);
exit(0);
}
兒童2:
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#define BUF_SIZE 16
int main(int argc, char* argv[]){
//Opens a file for reading/writing, if exists then truncates, otherwise makes new one
//with correct permissions
int wri_inFile = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IWUSR);
if(wri_inFile == -1)
perror("ERROR OPENING FILE");
//declaring variables
char buf[BUF_SIZE]; //to store what is read in/written out
int read_test; //test if EOF
for(;;) {
read_test = read(0, buf, BUF_SIZE); //read from buffer
if(read_test == 0) //eof
break;
write(wri_inFile, buf, BUF_SIZE); //write to file
}
close(wri_inFile);
exit(0);
}
創建文件的十六進制轉儲。它們可能是不同的,因爲在文件的開頭或結尾有一些不可見的字符/控制字符。 – GolezTrol
嘗試使用'cat -e'來查看是否有任何不可打印的字符。我敢打賭,在第二個結尾會有一個'\ 0'。 –
如果您使用'cmp'而不是'diff',會發生什麼? –