I’m stuck on Stage #JV1
I’ve tried to separate the full command into 2 parts : the text (toRedirect) and the file (redirectionLocation) then used ofstream to handle the > operator logic
Here are my logs:
[tester::#JV1] Running tests for Stage #JV1 (Redirection - Redirect stdout)
[tester::#JV1] [setup] export PATH=/tmp/orange/grape/pineapple:$PATH
[tester::#JV1] Running ./your_program.sh
[tester::#JV1] [setup] echo "banana" > "/tmp/owl/banana"
[tester::#JV1] [setup] echo "grape" > "/tmp/owl/grape"
[tester::#JV1] [setup] echo "pear" > "/tmp/owl/pear"
[your-program] $ ls -1 /tmp/owl > /tmp/ant/bee.md
[your-program] ls: /tmp/ant/bee.md: No such file or directory
[tester::#JV1] ^ Expected prompt ("$ ") but received "ls: /tmp/ant/bee.md: No such file or directory"
[your-program] ls: >: No such file or directory
[your-program] banana
[your-program] grape
[your-program] pear
[your-program] $
[tester::#JV1] Test failed
And here’s a snippet of my code:
/// Redirection
void redirect_output(const std::string& fileName, const std::string& text) {
std::ofstream outFile(fileName, std::ios::out);
if (outFile.is_open()) {
outFile << text << "\n";
outFile.close();
}
else {
std::cerr << "shell: " << fileName << ": " << std::strerror(errno) << std::endl;
}
}
Here is the echo command logic (inside main) :
// echo command
else if (command.find("echo ") == 0) {
std::string toPrint = command.substr(5);
size_t redirPos = toPrint.find('>');
if (redirPos != std::string::npos) {
// what to redirect
std::string toRedirect = toPrint.substr(0, redirPos);
// where to redirect
std::string redirectLocation = toPrint.substr(redirPos + 1);
// Clean up leading/trailing spaces from the text
if (!toRedirect.empty() && toRedirect.back() == ' ') {
toRedirect.pop_back();
}
// Remove leading spaces from the filename path
size_t firstNonSpace = redirectLocation.find_first_not_of(" ");
if (firstNonSpace != std::string::npos) {
redirectLocation = redirectLocation.substr(firstNonSpace);
}
redirect_output(redirectLocation, toRedirect);
}
else {
std::vector<std::string> arguments = handlQuotes(toPrint);
for (size_t i = 0; i < arguments.size(); i++) {
std::cout << arguments[i];
if (i < arguments.size() - 1) {
std::cout << " ";
}
}
std::cout << "\n";
}
}