c*****m 发帖数: 1160 | 1 这段函数本来很简单,只要string加起来就可以了:
bool dos2ufolder(std::string& path)
{
WIN32_FIND_DATA finfo;
string newpath = path + "abcde" // should be "\\*";
g_Log->Log("in dos2ufolder, the newpath is: %s", newpath.c_str());
...
}
它会被这个dos2u调用:
bool dos2u(const char * path)
{
struct stat s;
string Path(path);
if( stat(Path.c_str(),&s) == 0 )
{
if( s.st_mode & S_IFDIR )
{
dos2ufolder(Path);
}
else if( s.st_mode & S_IFREG )
{
return dos2ufile(path);
}
}
程序的里面调用这个函数,一直失败:得到的 newpath 跟 path都是相同的,没有加上
后面的字串。
然后我在 main()的最前端调用一个 test()函数:
void test(){
string path("c:\20120705700\Source");
dos2u("c:\20120705700\Source");
dos2u(path.c_str());
}
你能看见我在test()里面用 char*和 string.c_str()的方式调用了两次dos2u,结果都
是好的,newpath=c:\20120705700\Sourceabcde; 而程序跑下去之后,到了正常调用
dos2u的时候,newpath得到的值却是c:\20120705700\Source
我换了stringstream,还是一样:在test()里可以,在正常调用时不行。这两次调用都
是在同一次运行之中,怎么会得到不同的结果?太不可思议了!
谁有什么建议?先谢谢了。 |
c*****m 发帖数: 1160 | 2 唉,改成旧式strcat来做了,成功了。但是不能用string,真不爽。 |
l********a 发帖数: 1154 | 3 测试没问题啊,把字符串里面的\改成\\
测试代码
void dos2u(string& path)
{
string newpath = path+"abcd";
cout << newpath.c_str() << endl;
}
int main()
{
string path("c:\\test");
cout << path.c_str() << endl;
dos2u(path);
return 0;
} |
c*****m 发帖数: 1160 | 4
是啊,我知道正常时候这段程序是没问题的,而且我在程序main里加了test(),也成功
。可是为什么在程序运行当中会出错?我感觉是编译器优化搞得,但是我真的没有改动
什么设置。
【在 l********a 的大作中提到】 : 测试没问题啊,把字符串里面的\改成\\ : 测试代码 : void dos2u(string& path) : { : string newpath = path+"abcd"; : cout << newpath.c_str() << endl; : } : int main() : { : string path("c:\\test");
|