Weird behaviour of strcat()..

Aug 1, 2012 at 5:18am
1
2
3
4
5
6
7
int send(const char * ftp, const char * user, const char * pass, const char * pathondisk, char * nameonftp)
{
	char * buf=nameonftp;
	cout<<pathondisk<<endl;//Here i get the correct output
	strcat(buf,".txt");
	cout<<pathondisk<<endl;//but here i get only one single character
}

I have only pasted code that was causing problem, would you please tell me how this can happen? is there anything wrong the way i am using strcat(). Its weird because the 'pathondisk' is a const char and even though i applied strcat() on another string the contents of 'pathondisk' are altered.
Thanks
Regards
Last edited on Aug 1, 2012 at 5:19am
Aug 1, 2012 at 5:21am
Using strcat *modifies* the string. So when you get past line 5, nameonftp now has extra characters in it. I doubt that was what you intended.

You could probably avoid all this by just using std::string.
Aug 1, 2012 at 5:23am
I intended to add ".txt" to nameonftp that was stored to "buf", everything fine there but how come the contents of pathondisk changed?
thats what i am asking..
thanks for replying
Aug 1, 2012 at 5:29am
Well you aren't actually. You are making buf point to the same thing nameonftp points to, so when you modify buf, you are also modifying nameonftp.

If nameonftp isn't big enough to hold the extra characters, it will keep writing off into other address space, which might include some of pathondisk's characters, which is why it was modified.
Aug 1, 2012 at 5:33am
Thanks alot firedraco, problem solved.
Topic archived. No new replies allowed.