0

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?

3
  • 2
    Why did you try "C:\Documents and Settings\admin\testfile.foo"? It's obvious that C++ interprets the backslashes here as escape sequences. It's just confusing to have it as a part of the question... Now I don't understand what your question really is. :) Commented May 21, 2011 at 10:53
  • did you check to see if there are any non-printable characters in the generated path? Commented May 21, 2011 at 10:56
  • This is a good point. With my test function using isprint() it seems to break whenever it hits a space. I am using a URL decoding function because these file paths are URL encoded initially. Could it be because I'm typecasting %20 as char(20), resulting in a space, but ends up being an unprintable character? Commented May 21, 2011 at 11:12

2 Answers 2

2

There are so many wrong things in your code... Here is the number 1 problem:

temp2 = char(HexToInt(temp2));

temp2 is empty at this point, so HexToInt returns 0.

Here are more problem:

temp = Location[i+1] + Location[i+2]; 

this adds two char resulting in an int. It does not concatenate them. Use std::string::substr instead.

temp += j * pow(16.00, k);

don't use floating point like this.

P.S. and this just demonstrates that you code is more important than your problem description.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the holier than thou response, but you're correct. the URL decoder is the problem. I'll fix it up.
2

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 use c_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.

4 Comments

The embedded NULL is the problem here.
As I stated in the above comment my file locations are initially URL encoded. This didn't strike me as odd initially, but that may be the problem. Here is my decoding function: pastebin.com/xP3gDida I've tested the hex converter and it works just fine. The only thing I can think of is it's converting 20 (32d) to NULL instead of space because it's char() casted. What do you think?
Yep. Look at this line: temp2 = char(HexToInt(temp2)); You are converting temp2 (which is empty) and not temp.
I should also say that your function here is highly unoptimized. In such common cases (like URL-encoding parsing) it's usually the best to just copy a function from someone else, since he probably already tested it and optimized it. That saves you a lot of headache. Here's a non-tested, but slightly more optimized version of your code: pastebin.com/sWnjRDNX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.