如果这不能解决您的问题,您需要澄清您的问题。 - Richard Heathfield< http://www.cpax.org.uk> 电子邮件:-http:// www。 + rjh @ 谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php> Usenet是一个奇怪的放置" - dmr 1999年7月29日char *str1 = "d:\\temp\\data\\test.txt";If that doesn''t fix your problem, you need to clarify what your problem is.--Richard Heathfield <http://www.cpax.org.uk>Email: -http://www. +rjh@Google users: <http://www.cpax.org.uk/prg/writings/googly.php>"Usenet is a strange place" - dmr 29 July 1999 str1指向的字符串是字符串文字,你不应该尝试修改它。字符串文字通常是不可修改的,并且 任何情况下你的新字符串占用的空间比字符串字面数多, 所以即使你可以修改它,你也会超出结束它。 你需要为新字符串分配一个足够空间的数组 (包括终止零字节)然后像 / *警告:既没有测试也没有写成防弹 (甚至是防摔)* / 无效替换( char * source,char * target) { for(; * source; source ++) { if(* source =''\\'')* target ++ = * source; * target ++ = * source; } } 现在,上面的代码在看到它时只产生两个''\\'。 但几乎在任何系统上,它都是更有意义的是将''\\''替换为''/'' (甚至windows和dos都没有问题。他的),当上面的 然后变成 void replaceceslash(char * source,char * target) { for(; * source;来源++) { if(* source =''\\'')* target ++ =''/''; else * target ++ = * source; / *注意别的! * / } }The string to which str1 points is a string literal, and you should notattempt to modify it. String literals are usually not modifiable and inany case your new string takes more space than the string literal does,so even if you could modify it, you would overrun the end of it.You need to allocate an array of sufficient space for the new string(including the terminating zero-byte) and then something like/* warning: neither tested nor written to be bulletproof(or even dart-resistant) */void replaceslash(char *source, char *target){for (;*source; source++){if (*source = ''\\'') *target++ = *source;*target++ = *source;}}Now, the code above simply produces two ''\\'' when it sees one.But on almost any system, it makes more sense to replace ''\\'' with ''/''(even windows and dos have no problem with this), when the abovethen becomesvoid replaceslash(char *source, char *target){for (;*source; source++){if (*source = ''\\'') *target++ = ''/'';else *target++ = *source; /* notice the else! */}} 这篇关于替换子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-13 06:07