2017-01-02 29 views
-1

我不知道這裏發生了什麼。這裏是代碼示例:CPP中的「SetCurrentDirectory」(「windows.h」)中的一些不理解的東西

#include<stdio.h> 
#include<iostream> 
#include<string> 
#include <Windows.h> 
using namespace std; 
int main() 
{ 
    char my_current_path[1024]; 
    string current_path(R"(C:)"); 
    if (!SetCurrentDirectory(current_path.c_str())) 
     cout << "cant change to that directory."; 
    GetCurrentDirectory(1024, my_current_path); 
    std::cout << my_current_path << endl; 
    system("pause"); 
    return 1; 
} 

我在這裏做的是試圖將目錄更改爲某些目錄。

我的代碼有兩件奇怪的事情。

(1奇怪的事情)

當我嘗試更改爲 「c:」 這樣的:

#include<stdio.h> 
#include<iostream> 
#include<string> 
#include <Windows.h> 
using namespace std; 
int main() 
{ 
    char my_current_path[1024]; 
    string current_path(R"(C:)"); 
    if (!SetCurrentDirectory(current_path.c_str())) 
     cout << "cant change to that directory."; 
    GetCurrentDirectory(1024, my_current_path); 
    std::cout << my_current_path << endl; 
    system("pause"); 
    return 1; 
} 

它不工作,不改變路徑(和它的好事。 )但它不顯示當目錄沒有改變時顯示的消息:

cout << "cant change to that directory."; 

這是爲什麼? (當我嘗試改變像「efef:」或「存在」之類的東西時,它顯示我是messege。但是爲什麼在這裏它不顯示我,並且還嘗試更改當前的工作目錄?

奇怪的事情)

當我目錄更改爲「G:」它由於某種原因,工作..

#include<stdio.h> 
#include<iostream> 
#include<string> 
#include <Windows.h> 
using namespace std; 
int main() 
{ 
    char my_current_path[1024]; 
    string current_path(R"(G:)"); 
    if (!SetCurrentDirectory(current_path.c_str())) 
     cout << "cant change to that directory."; 
    GetCurrentDirectory(1024, my_current_path); 
    std::cout << my_current_path << endl; 
    system("pause"); 
    return 1; 
} 

該代碼編譯,表明我成功地改變了目錄,以「G」。 改變到那條路徑之後,我試圖改變爲「C:」(之前沒有做任何事情),現在它工作!但奇怪的是,它不是移動到「C:\」但到:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE 

而且它怪異的,以前它沒有工作,現在當我在「G:」路徑,並試圖轉移到「c:」再次,然後它走向我不想要的路徑!

+1

'如果函數失敗,則返回值爲零。要獲得擴展的錯誤信息,請調用GetLastError.'因此,添加一些錯誤檢查,以便知道它爲什麼失敗。 –

+1

爲什麼'「(C :)」'你只能'「C:\\」' – Raindrop7

+0

@RetiredNinja它沒有改變目錄,但沒有進入if(有一個如果案件它返回零 –

回答

4

,你必須包含反斜槓,即C:\

您正在使用的語法C:具有不同的含義。命令行程序將每個驅動器上當前驅動器和當前目錄的概念分開,以便您可以快速切換驅動器,而無需每次都重新鍵入完整路徑。由於向後兼容性原因(見下文)引入了此功能(很久以前),但實際上非常有用;高級用戶有時會將多個驅動器號映射到相同的驅動器或網絡共享中,以便充分利用此功能。

儘管只有命令shell跟蹤每個驅動器的當前目錄,但Win32 API知道如何查找它們,並且每當指定驅動器但沒有路徑時都會這樣做。作爲一種特殊情況,如果指定的驅動器是當前驅動器,則會擴展到實際的當前目錄而不是保存的當前目錄。

因此,在您的示例中,不完整路徑C:已擴展到C驅動器的當前目錄;由於您的當前目錄已經在C驅動器上,因此SetCurrentDirectory(R"(C:)")是無操作的。如果您想更改爲根目錄,則應該使用SetCurrentDirectory(R"(C:\)")

在您的測試中,G:驅動器沒有保存的當前目錄,它不是當前驅動器,因此其效果是將當前目錄設置爲G:\,即您獲得了預期的行爲,但不是你期待的原因。如果您已經從命令行程序啓動了該程序,並且當前目錄已經保存爲G驅動器,那麼您已經完成了該任務。

同樣的事情適用於打開文件; C:file.txt將從C驅動器的當前目錄打開file.txt,而C:\file.txt將從根目錄打開file.txt

請參閱Raymond Chen的博客文章Why does each drive have its own current directory?以瞭解此功能的歷史記錄。

0

可用於讀取和設置當前目錄下的兩個API:GetCurrentDirectory()SetCurrentDirectory()只提供用於後者的有效路徑:如果要指定一個驅動器的根目錄

#include <iostream> 
#include <windows.h> 
using namespace std; 


int main() 
{ 
    char czCurDir[MAX_PATH] = ""; 
    char czNewDir[MAX_PATH] = ""; 

    // retrieve the current directory 
    GetCurrentDirectory(MAX_PATH, czCurDir); 
    cout << "Current directory: " << czCurDir << endl;// C:\Users\Raindrop7\Desktop\New folder 

    // set the value for the new directory NB: C:\\ not C:\ because the first backslah is an escape character 
    strcpy(czNewDir, "C:\\"); 

    // set the current directory to "C:\" 
    SetCurrentDirectory(czNewDir); 

    // retrieve the current directory 
    GetCurrentDirectory(MAX_PATH, czCurDir); 

    cout << "current directory: " << czCurDir << endl;// C:\ 


    return 0; 
} 
相關問題