2012-07-13 98 views
0

我已經給出了一個靜態庫來處理接受參數作爲空格分隔字符的靜態庫。IOS文件路徑空間在傳遞給庫方法時被分割

在庫法

int saveFile(char* param); 

我傳遞給它的文檔文件路徑保存到

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
std::string str = [documentsDirectory cStringUsingEncoding:[NSString defaultCStringEncoding]]; 
const char * filePath = str.c_str(); 
char pa[1024]; 
pa[0] = 0; 
strcat(pa, filePath); 
saveFile(pa); 

我的問題是,IOS文件路徑中有空格在裏面,這將導致圖書館將這些地方的路徑分開。我嘗試用「\」來轉義空格,當然,在這種情況下將路徑放在引號中不起作用。例如下面...

/Users/bigbadowl/Library/Application Support/iPhone Simulator/5.1/Applications/649D2EEB-8C88-42C7-9A74-21629570B1D0/Documents 

中會被分割到

/Users/bigbadowl/Library/Application 
Support/iPhone 
Simulator/5.1/Applications/649D2EEB-8C88-42C7-9A74-21629570B1D0/Documents 

任何想法,將不勝感激。

由於

+0

你是什麼意思_split the path_?三個拆分字符串在哪裏存儲? – 2012-07-13 22:32:49

+0

這是圖書館的根本錯誤,所有有空間的東西都會被分割。我已經設法讓我的手在源代碼上,並改爲使用逗號分隔。 – BigBadOwl 2012-07-14 06:48:59

回答

0

一種解決方案是用一個不太可能要被使用的字符來替換原始字符串的空間(如#),執行庫操作,然後還原該字符的所有出現回到空間。一些沿線的:

// ... 
std::replace(str .begin(), str .end(), ' ', '#'); // Replace spaces with # 
const char * filePath = str.c_str(); 
char pa[1024] = {0}; 
strcat(pa, str.c_str()); 
std::replace(str .begin(), str .end(), '#', ' '); // Replace # with spaces 
// ... 

當然,如果原始字符串包含此字符,您將無法獲得所需的行爲。你可以隨時測試這個並選擇另一個字符,所以這應該不是問題。

+0

這是最接近的答案,根據我上面的評論。謝謝 – BigBadOwl 2012-07-14 06:50:01

0

我認爲這隻會是模擬器的問題。如果您無法更改saveFile,那麼您可能需要查看是否可以欺騙模擬器。

作爲一個測試,看看你是否可以創建一個符號鏈接/Users/bigbadowl/Library/Application Support/iPhone Simulator/Users/bigbadowl/名爲iphonesim。然後用iphonesim代替Library/Application Support/iPhone Simulator

您將以不帶空格的路徑/Users/bigbadowl/iphonesim/5.1/Applications/649D2EEB-8C88-42C7-9A74-21629570B1D0/Documents結束。看看saveFile()可以用。

祝你好運。