IOS文件路径空间在传递给库方法时被分割
我已经给出了一个静态库来处理接受参数作为空格分隔字符的静态库。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
任何想法,将不胜感激。
由于
一种解决方案是用一个不太可能要被使用的字符来替换原始字符串的空间(如#
),执行库操作,然后还原该字符的所有出现回到空间。一些沿线的:
// ...
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
// ...
当然,如果原始字符串包含此字符,您将无法获得所需的行为。你可以随时测试这个并选择另一个字符,所以这应该不是问题。
这是最接近的答案,根据我上面的评论。谢谢 – BigBadOwl 2012-07-14 06:50:01
我认为这只会是模拟器的问题。如果您无法更改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()
可以用。
祝你好运。
你是什么意思_split the path_?三个拆分字符串在哪里存储? – 2012-07-13 22:32:49
这是图书馆的根本错误,所有有空间的东西都会被分割。我已经设法让我的手在源代码上,并改为使用逗号分隔。 – BigBadOwl 2012-07-14 06:48:59