文件路径的 std::string 到 str::string.c_str() 转换中的有趣问题
我遇到了一个有趣的问题。我有以下代码:
cout << "\nFILE";
cout << "\tLocation:" << file.location << endl;
cout << "\tLocation (c_str()): " << file.location.c_str() << endl;
其中位置由一个函数设置,该函数在具有以下格式的文件中查找文件位置
驱动器:\dir1\dir2...\文件名.扩展名
例如,该函数将成功将 file.location 设置为
C:\Documents and Settings\admin\testfile.foo
然而,最奇怪的事情发生了。它输出如下所示的内容:
文件
位置:C:\Documents and Settings\admin\testfile.foo
位置 (c_str()):C:\Documents
请注意缺少剩余文件路径。作为一个精明的程序员,我决定测试绝对路径。我将字符串 file.location 物理设置为
C:\\Documents and Settings\\admin\\testfile.foo
相应的输出是
文件
位置:C:\Documents and Settings\admin\testfile.foo
位置 (c_str()):C:\Documents and Settings\admin\testfile.foo
如预期。然后我测试了
C:\Documents and Settings\admin\testfile.foo
输出为
文件
位置:C:Documents and Settingsadmintestfile.foo
位置 (c_str()):C:Documents and Settingsadmintestfile.foo
也是预期的。
我一生都无法弄清楚可能出了什么问题。字符串本身的文件路径显然是正确的,为什么只有在这种情况下才会改变?
I've ran into an interesting problem. I have the following code:
cout << "\nFILE";
cout << "\tLocation:" << file.location << endl;
cout << "\tLocation (c_str()): " << file.location.c_str() << endl;
where location is set by a function that finds a file location in a file with the format
DRIVE:\dir1\dir2...\filename.extension
For example, the function will have successfully set file.location to
C:\Documents and Settings\admin\testfile.foo
However, the strangest thing happens. It outputs something that looks like this:
FILE
Location: C:\Documents and Settings\admin\testfile.foo
Location (c_str()): C:\Documents
Note the lack of the remaining file path. Being the astute programmer I am, I decided to test absolute paths. I physically set the string file.location to
C:\\Documents and Settings\\admin\\testfile.foo
and the corresponding output was
FILE
Location: C:\Documents and Settings\admin\testfile.foo
Location (c_str()): C:\Documents and Settings\admin\testfile.foo
as expected. I then tested
C:\Documents and Settings\admin\testfile.foo
and the output was
FILE
Location: C:Documents and Settingsadmintestfile.foo
Location (c_str()): C:Documents and Settingsadmintestfile.foo
also expected.
I cannot for the life of me figure out what could possibly be going wrong. The file path is clearly correct in the string itself, why would it change only in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中有很多错误...这是第一个问题:
temp2 此时为空,因此
HexToInt
返回 0。这里还有更多问题:
这会添加两个
char
,从而产生int
。它不连接它们。使用std::string::substr
代替。不要像这样使用浮点数。
PS,这只是表明你的代码比你的问题描述更重要。
There are so many wrong things in your code... Here is the number 1 problem:
temp2
is empty at this point, soHexToInt
returns 0.Here are more problem:
this adds two
char
resulting in anint
. It does not concatenate them. Usestd::string::substr
instead.don't use floating point like this.
P.S. and this just demonstrates that you code is more important than your problem description.
我不太确定我明白你到底在问什么,但我有一个建议可以让你在操作路径时省去很多麻烦:使用 Boost.Filesystem.Path。它可能也能解决您在这里遇到的问题。 :)
现在,对于第一种情况 - 如果我理解正确的话,
file.location
是一个 std::string。如果您直接将其写入流,它会为您提供完整的字符串,但如果您使用c_str()
,则字符串会在中间被剪切。这可能意味着您的字符串中间、文档后面有一个 NULL 字符。我不知道为什么会这样,但如果您可以在此处发布实际设置 file.location 的代码,我们也许可以帮助您。I'm not quite sure I understand what exactly you're asking here, but I have a suggestion that can save you a lot of headache when manipulating paths: use Boost.Filesystem.Path. It will probably solve the problem you're having here as well. :)
Now, for your first case - if I understand correctly,
file.location
is an std::string. If you write it directly to a stream, it gives you the full string, but if you usec_str()
, the string gets cut in the middle. That probably means you've got a NULL character in the middle of your string, after the document. I don't know why is that, but if you could post here the code that actually sets file.location, we may be able to help you.