C++ - Build your own shell - Stage #TG6: The file does not open despite the filepath being correct

I’m stuck on Stage #TG6.

I’ve tried merging the given file name with the current directory path as well but nothing seems to work.

Here are my logs:

remote: [tester::#TG6] Writing file "/tmp/bar/f 2" with content "orange raspberry."
remote: [tester::#TG6] Writing file "/tmp/bar/f   95" with content "pineapple strawberry."
remote: [tester::#TG6] Writing file "/tmp/bar/f's18" with content "strawberry pear."
remote: [your-program] $ cat "/tmp/bar/f 2" "/tmp/bar/f   95" "/tmp/bar/f's18"
remote: [your-program] cat: tmp/bar/f 2: No such file or directory
remote: [tester::#TG6] Output does not match expected value.
remote: [tester::#TG6] Expected: "orange raspberry.pineapple strawberry.strawberry pear."
remote: [tester::#TG6] Received: "cat: tmp/bar/f 2: No such file or directory"
remote: [your-program] cat: tmp/bar/f   95: No such file or directory
remote: [your-program] cat: tmp/bar/f's18: No such file or directory
remote: [your-program] $
remote: [tester::#TG6] Assertion failed.
remote: [tester::#TG6] Test failed

And here’s a snippet of my code:

void handleCat(std::string argument)
{
  std::vector<std::string> files;
  char check = '\0';
  std::string file_path = "";
  for (int i = 0; i < argument.size(); i++)
  {
    if(check == 'd' && argument[i] == '\"')
    {
      if(check == 'd')
        check = '\0';
      if (i > 0)
        files.push_back(file_path);
      file_path = "";

    }
    else if (check == 'c' && argument[i] == '\'')
    {
      if(check == 'c')
        check = '\0';
      if (i > 0)
        files.push_back(file_path);
      file_path = "";
    }
    else if(check == '\0') {
      if(argument[i] == ' ' && file_path.size() > 0) {
        files.push_back(file_path);
        file_path = "";
      }
      else if(argument[i] == '\"') {
        check = 'd';
      }
      else if(argument[i] == '\'') {
        check = 'c';
      }
      else if(argument[i] != ' ') {
        file_path += argument[i];
      }
    }
    else if (check == 'c' || check == 'd')
    {
      file_path += argument[i];
    }
  }
  if(file_path.size() > 0)
    files.push_back(file_path);
  for (int i = 0; i < files.size(); i++)
  {
    std::string ifile = files[i];
    if(ifile[0] == '/')
      ifile = ifile.substr(1);
    std::ifstream file(ifile);
    if (file.is_open())
    {
      std::stringstream buffer;
      buffer << file.rdbuf();
      std::string fileContent = buffer.str();
      std::cout << fileContent;
      if(i == files.size() - 1)
        std::cout << std::endl;
      file.close();
      }
    else
    {
      std::cerr << "cat: " << ifile << ": No such file or directory" << std::endl;
    }
  }
}

Hi @Coder-crooz-v2, looks like you’ve got past this stage — do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

Yeah, I missed the '' in front of tmp

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.