由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Visual Studion 10, C++ string问题:表现不一致
相关主题
how to write some C/C++ program to enable dual monitor?windows下mfc,dll, etc.
请教C++程序中手动Ctrl+C后,如何才可以调用一下自己写的退出函数VISUAL STUDIO 2005 输出窗口(output) 怎么找不到?
C++ string类输入数据的问题请问一个visual studio的菜鸟问题
一个有关visual stdio 2005的问题Recommend a C++ IDE?
Visual C++ Express 很土的问题求救受不了拉 - 技术发展太快!!! 学啥才不过时? (转载)
在visual C++ 程序里播放mp3如何在VC++下把raw图像快速写到硬盘里呢?
你妈用VS写个helloworld真难啊。Help - C++ Debug Assertion Failed
win32 c编程能否调用mfc代码?multi-thread 一问,
相关话题的讨论汇总
话题: path话题: dos2u话题: newpath话题: string话题: 调用
进入Programming版参与讨论
1 (共1页)
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");

1 (共1页)
进入Programming版参与讨论
相关主题
multi-thread 一问,Visual C++ Express 很土的问题求救
调用win32 DLL的问题在visual C++ 程序里播放mp3
About command line in C++你妈用VS写个helloworld真难啊。
在C/C++里的文件复制操作win32 c编程能否调用mfc代码?
how to write some C/C++ program to enable dual monitor?windows下mfc,dll, etc.
请教C++程序中手动Ctrl+C后,如何才可以调用一下自己写的退出函数VISUAL STUDIO 2005 输出窗口(output) 怎么找不到?
C++ string类输入数据的问题请问一个visual studio的菜鸟问题
一个有关visual stdio 2005的问题Recommend a C++ IDE?
相关话题的讨论汇总
话题: path话题: dos2u话题: newpath话题: string话题: 调用