Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to combine two strings

Status
Not open for further replies.

trent101

Programmer
Nov 4, 2005
50
AU
Hey,

I am writing a win32 consol app using visual c++.

what I am trying to do is cobine two strings. this is how i am doing it:

String one = ".//savefile//";
string two = "fileone.txt";

string three = one + two;

Now this compiles and runs, but i get a weird error, insetad of the string three reading ".//savefile//fileone.txt", it instead reads "Xuo".

Does anyone know why this is happening? Am I doing it the wrong way?

Thanks for your time.
 
The C++ is a case-sensitive language. What is it: String one?
Is it a right Windows file name ".//savefile//fileone.txt"?..
Are you sure that info as above is helpful to help you?..
 
ok

string a = "./orders/";
string b = "10_12_06";
string c = a + b;

now should string c read "./orders/10_12_06"?

when I compile, i now get a even more weird result. string c now reads "e0".

why is this? is there something weird going on?

 
Post some actual code of how you use this.

If you're trying
[tt]in.open(c);[/tt]
You're doing it wrong.

Perhaps you could try
[tt]in.open(c.c_str());[/tt]

Basically, it looks like you're getting the address of something rather than the string itself.

If your compiler is producing any warnings at all, then fix them (this does not mean apply indiscriminate casts just to make the compiler shut up).

--
 
ok,

what i am trying to do is this.

I want to create a file, and its name has to be what ever the value of my string is. I also wish to store it in a folder called "orders".

So I tried ->

string path = ".//orders//";
string filename = "10_06_06"

string file = path + filename;

ofstream myfile(file.c_str());


now, it compiles with no errors or warnings. But when i run it the file is not created. However when I debug and step onto the line "string file = path + filename", instead of getting ".//orders//10_06_06", i get something now i cant even type. I get a 'sqaure' followed by "e0".

Why cant I combine these two strings. To make things even more weird, if i change the path string to ".//or//", the string actually combines properly and gives me no weird result.

can enyone explain what is going on? I this the wrong way to combine the strings?
 
Maybe while you're debuggin you're inspecting the object and not its content.

Cheers,
Dian
 
can someone at least try this code and tell me they dont get the same result?

when i look at the value field of my c variable, i see:

{0x0012f6do "e0"} but there is a square in front of the e that i cannot type.
 
Try doing:
Code:
cout << file << endl;
If that still prints "e0" then there are some serious problems with your compiler.
BTW, what compiler are you using?

You're also using the wrong path symbol. In windows, you only need 1 slash '/' or 2 backslashes '\\'.
 
The string class in the standard library that ships with Visual C++ for at least one version has two different places that the string is held. Short strings are held in a small static buffer, and longer strings are held via pointer to dynamically allocated memory.

What that means for someone debugging is that the string that shows up in the watch window isn't always the actual data being held by the string object. Use cout to test it's actual value as others have suggested, or expand the string in the watch window until you can see multiple data members and see if the string you expect is there.
 
Code:
string path = ".//orders//";
string filename = "10_06_06"
string file = path + filename;
It's NOT a correct Windows file name: .//orders//10_06_06.
I think you want backslashes? Try them...
 
It's NOT a correct Windows file name: .//orders//10_06_06.
I think you want backslashes?

It's possible to use slashes in file names. Windows file I/O functions in file names accept them as backslashes. It should be a Windows/Unix compatibility issue.
 
Yes, but you should only have 1 slash, not 2. The only reason you need 2 backslashes is because the backslash is an escape character, so the 2nd one tells the compiler you want an actual backslash character.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top