"Arctic Fidelity" <> writes:
> On Fri, 28 Oct 2005 17:09:43 -0400, <> wrote:
>
>> void copy(char *filename)
>> {
>> char *log_file;
>> log_file=filename;
>> strcat(log_file,"-log.txt");
>>
>> //.......
>> }
>>
>> suppose that filename="myfile.dat"
>>
>> I'm expecting:
>> log_file="myfile.dat-log.txt"
>> and this work fine....
>>
>> the problem is that I need to remain filename as the original name but
>> instead i have:
>> filename="myfile.dat-log.txt"
>>
>> How can I do to avoid this, and preserve the original name???
>
> When you assign the pointer of filename (assuming that's how you
> properly declared that), to log_file, then you're telling log_file to
> point to the same space in memory that filename is. If you use
> strcat, you're feeding that pointer into the function, and it is
> concatenating information to that point. Since both filename and
> log_file are pointing to the same space in memory, the same space is
> going to be written.
>
> What you want to do is create a copy of the memory pointed to by
> filename, and assign log_file to the copy. That is, you have to have
> two different spaces in memory, so that you can copy the space
> pointed to by filename to the space pointed to by log_file, and then
> you can change the space pointed to by log_file without changing the
> space of filename, because filename is pointing to another
> space. Make sense?
That's basically correct, but there are some serious problems in your
code. You should try compiling and executing it before posting.
> Think:
>
> #include <string.h>
Since you use printf(), you also need a "#include <stdio.h>".
Since you use malloc(), you also need a "#include <stdlib.h>".
> #define STRGSIZE 50
Why 50, especially since you can figure out exactly how much space is
actually needed?
> void main(void)
No, no, no, no, no.
main() returns int, not void.
> {
> char *log_file;
> char filename[] = "test";
>
> log_file = malloc(STRGSIZE);
Always check the result of malloc(); if it fails, it will return a
null pointer. Often the only thing you can do in response is to abort
the program, but it's better than continuing blindly.
You're trying to concatenate two strings. You know the length of each
of them, therefore you know exactly how much space you need for the
concatenation.
At this point, log_file points to an uninitialized block of 50 bytes.
There's no guarantee that this block contains a valid string, so
passing it to strncat() invokes undefined behavior.
The value of log_file is supposed to be "test-log.txt", but you never
copy the value "test" into log_file.
> strncat(log_file, "-log.txt", STRGSIZE);
> printf("filename: %s\nlog_file: %s\n", filename, log_file);
> }
Finally, you should have a "return 0;" at the end of your main
function. It's not required in C99, but it can't hurt, and it's
considered good style.
Try this:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *filename = "test";
const char *suffix = "-log.txt";
char *log_file;
size_t log_file_len = strlen(filename) + strlen(suffix) + 1;
log_file = malloc(log_file_len);
if (log_file == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
strcpy(log_file, filename);
strcat(log_file, suffix);
printf("filename = \"%s\"\n", filename);
printf("suffix = \"%s\"\n", suffix);
printf("log_file = \"%s\"\n", log_file);
return 0;
}
Note that the original question assumed a function that takes the
filename as an argument; neither your program nor my modified version
of it does this. Probably the function should take a char* argument
and return a char* result. Returning a dynamically sized string can
be complicated; either you have to assume a maximum size, or the
caller has to allocate the space (which can be difficult if the caller
doesn't know how much space will be required), or the function has to
allocate the space (making the caller responsible for deallocating
it). I'm going to leave the code as it is for now, but the original
poster should feel free to ask followup questions.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.