如图所示的项目目录结构

如果要在main.cpp里用相对路径保存一个txt文件

   std::ofstream file("./tree_model/my_file.txt");
   if (file.is_open()) {
      file << "This is a sample text.\n";
      file.close();
      std::cout << "File saved in the model folder." << std::endl;
   } else {
      std::cerr << "Failed to open the file." << std::endl;
   }

路径:./代表的是 launch.json文件中 "cwd": 的设置,一种写法是:"cwd": "${workspaceFolder}"

workspaceFolder代表的时工作空间目录

还有一种写法是"cwd": "${fileDirname}",

fileDirname代表的是该cpp文件所在的目录,即main.cpp所在的src目录。

CMakeLists.txt文件所在目录可以这样表示

${CMAKE_CURRENT_SOURCE_DIR}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐