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?
There are so many wrong things in your code… Here is the number 1 problem:
temp2is empty at this point, soHexToIntreturns 0.Here are more problem:
this adds two
charresulting in anint. It does not concatenate them. Usestd::string::substrinstead.don’t use floating point like this.
P.S. and this just demonstrates that you code is more important than your problem description.