-
Problem with new :/
Code:
char* file_location = new char;
strncat (file_location, filename, strlen(filename) - strlen(basename(filename)));
strcat (file_location, "../conf/sync.conf");
file = new char;
strcpy (file, file_location);
Simply enough, i takes file_location, copies filename into it minus the filename, and then adds "../conf/sync.conf"
(filename is a string to the absolute path of the program being ran)
But, when the "file = new char;" is called, it cuts down the string file_location a lot, making the file sync.conf unopenable o_0;
Why is new on a variable killing a different one? o_0
Can I only have one new at a time.. or.. ? x_X
Thanks...
Funny, doing this in the malloc way works fine =(
-
You are only allocating one char. You need to allocate room for the entire string. You do this by:
Code:
char* file_location = new char[100];
Obviously 100 can be replaced by however many char's you need to allocate. Also, when you use new with [] you need to use delete with [].
delete [] file_location.
-
Ahh, I thought that it didn't need that =)
Thank yas
One question.. why would all the other ones work ok until this point?
Edit: Also, what if I want to change the size of it? Delete it first then do another new?
-
What do you mean "all the other ones"? malloc? I'm not sure what you mean there..
As for your edit, yes, you would have to delete it then new another.
-
A better idea would be to use std::string.
-
I ment all the strings, even though there wasn't the space for it, still printed out and all... with new, not talking about malloc
I don't even know what std::string is :P I haven't played with iostream.h, if thats what it is :O (I rarely use anything that I can't find on my unix man pages :P)