DEV Community

Cover image for VSCode C++ extension 執行程式時的工作路徑
codemee
codemee

Posted on

1

VSCode C++ extension 執行程式時的工作路徑

今天測試一個小程式, 像是這樣:

// Ch18_4, 將資料寫入文字檔
#include <fstream> // 載入fstream標頭檔
#include <iostream>
using namespace std;
int main(void)
{
   // 建立ofile物件
   ofstream ofile(".\\donkey.txt", ios::out);
   if (ofile.is_open())
   {                                   // 測試檔案是否被開啟
      ofile << "我有一隻小毛驢" << endl; // 將字串寫入檔案
      ofile << "我從來也不騎" << endl;  // 將字串寫入檔案
      cout << "已將字串寫入檔案..." << endl;
   }
   else
      cout << "檔案開啟失敗..." << endl;

   ofile.close(); // 關閉檔案
   return 0;
}
Enter fullscreen mode Exit fullscreen mode

在 VSCode 中執行後就發現我找不到儲存的 donkey.txt 檔, 原本以為不就是在我現在工作目錄下, 發現沒有, 仔細看一下 VSCode 中 C++ extension 幫我執行程式的指令:

#  & 'c:\Users\meebo\scoop\apps\vscode\1.90.2\data\extensions\ms-vscode.cpptools-1.20.5-win32-x64\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-s1dizyvd.jxy' '--stdout=Microsoft-MIEngine-Out-r0d523uu.x0s' '--stderr=Microsoft-MIEngine-Error-man3cnzk.ub0' '--pid=Microsoft-MIEngine-Pid-gmbauy4i.3uu' '--dbgExe=C:\Users\meebo\scoop\apps\tdm-gcc\current\bin\gdb.exe' '--interpreter=mi'
Enter fullscreen mode Exit fullscreen mode

實際執行我的程式的是 gdb 除錯器, 所以到除錯器的路徑下尋找, 果然:

# ls C:\Users\meebo\scoop\apps\tdm-gcc\current\bin\*.txt

    Directory: C:\Users\meebo\scoop\apps\tdm-gcc\current\bin

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---         2024/6/21 上午 11:15             43 donkey.txt
Enter fullscreen mode Exit fullscreen mode

找到我的小毛驢了!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs