Я работаю над домашним заданием для своего класса UNIX & Network Programming, где я должен переписать функциональность UNIX pipe (|).
Я пробовал несколько вещей, таких как изменение частей моего кода набезрезультатно и использую GDB для отладки кода, но это никуда меня не указывает. Я сделал некоторую проверку ошибок, включая выход из программы (цикла), если пользователь вводит нулевое значение, но это, похоже, не помогло.
// Variable declarations.
char command1[255], command2[255];
char *tok1, *tok2;
string file_path;
int pipefd[2], rs;
// Loop indefinitely.
while(1) {
// Get user input and store it.
// command 1
cout << "command1? ";
cin.getline(command1, 255);
// tokenize the command based on space as the delimiter
tok1 = strtok(command1, " ");
// if user typed quit, stop the loop.
if((string)tok1 == "quit") break;
// if user left field blank, error and stop the loop.
if(tok1==NULL) { cerr << "pipe: Must enter a command.\n"; break; }
// command 2
cout << "command2? ";
cin.getline(command1, 255);
// tokenize the command based on space as the delimiter
tok2 = strtok(command2, " ");
// if user typed quit, stop the loop.
if((string)tok2=="quit") break;
// if user left field blank, error and stop the loop.
if(tok2==NULL) { cerr << "pipe: Must enter a command.\n"; break; }
}
... цикл заканчивается после конвейера иРазветвление и все, что сделано, сокращено до того места, где, по моему мнению, проблема.
Появляется следующая ошибка, и я не могу понять, как ее исправить.
command1? ls
command2? wc
завершить вызов после выброса экземпляра 'std :: logic_error'
what (): basic_string :: _ M_construct null not valid