簡體   English   中英

如何處理boost :: filesystem :: path的空間

[英]How to deal with spaces for boost::filesystem::path

我正在嘗試從用戶輸入中獲取目錄,並將其存儲在boost庫中的路徑對象中。 如果目錄中沒有空格,例如C:\\ Windows \\ system32 \\ file.exe,則此方法運行良好,但是當嘗試使用C:\\ Program Files \\ file.exe時,該方法無效,程序僅退出。 我正在考慮將輸入作為字符串,然后對其進行操作以用轉義符替換空格。 有一個更好的方法嗎?

boost::filesystem::path path;
std::cout << "Please enter the path for the file you would like to hash:" << std::endl;
std::cout << "E.g. C:\\Program Files\\iTunes\\iTunes.exe" << std::endl;
std::cin >> path;   

然后將路徑傳遞給函數以進行哈希處理。 對於沒有空格但只有空格的程序來說,該程序可以正常退出。

std::string md5_file(boost::filesystem::path &file)
{
/* Takes a file and returns the md5 hash. */

// Create new hash wrapper
hashwrapper *myWrapper = new md5wrapper();
std::string hash;

// Hash file
try 
{
    hash = myWrapper->getHashFromFile(file.string());
}
catch (hlException &e) 
{
    std::cerr << "Error(" << e.error_number() << "): " << e.error_message() << std::endl;
}

// Clean up
delete myWrapper;
return hash;
}

您的問題與boost :: filesystem :: path無關。 您的輸入有問題。 如果路徑中有空格cin >> string_variable將讀取第一個空格分隔符。

嘗試檢查一下:

[boost::filesystem::path][1] path;
std::cout << "Please enter the path for the file you would like to hash:" << std::endl;
std::cout << "E.g. C:\\Program Files\\iTunes\\iTunes.exe" << std::endl;
std::cin >> path;
std::cout << path << endl;

輸出應為C:\\\\Program

std :: getline讀取整個帶有空格的字符串:

string str;
getline(cin, s);
path = s;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM