char* linker

Started by
10 comments, last by programering 21 years, 1 month ago
I have tried to link together three strings(char *), but it didn''t work when I wrote like this: char *path = "path/"; char *name = "file"; char *format = ".dat"; char *adress; adress = path + name + format; // or when I did like this: #define STRING_LINKER(a,b,c) ((a) ## (b) ## (c)) adress = STRING_LINKER(path,name,format); How shall I do it, is there any function that handles this? Anton Karlsson Klingis Entertainment Games with silly humor
Advertisement
strcat and strcpy are what you need!
an example please!

Anton Karlsson
Klingis Entertainment
Games with silly humor
The c way would use sprintf or strcat.
  char *path = "path/";char *name = "file";char *format = ".dat";char *address = (char*)malloc(strlen(path) + strlen(name) + strlen(format) + 1);sprintf(address, "%s%s%s", path, name, format);// do stuff with addressfree(address);  
With c++-style strings, the code becomes:
  #include <string>using std::string;string path = "path/";string name = "file";string format = ".dat";string address = path + name + format;  
...I tend to prefer the latter.

[edited by - Beer Hunter on April 1, 2003 6:16:40 AM]
But with strcat and strcpy!

Anton Karlsson
Klingis Entertainment
Games with silly humor
don''t you have any documentation with your compiler?
No, not for those functions.
strcat

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strcat.2c_.wcscat.2c_._mbscat.asp

strcpy

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strcpy.2c_.wcscpy.2c_._mbscpy.asp

Goolge

www.Goolge.com

[edited by - Drazgal on April 1, 2003 6:50:20 AM]
Like this then?
Correct me if I''m wrong here.

char *path = "path/";
char *name = "file";
char *format = ".dat";

char *adress;
adress = (char *)
malloc(sizeof(path) + sizeof(name) + sizeof(format));

strcpy(adress,path);
strcat(adress,name);
strcat(adress,format);


What does strcat stands for short?




Anton Karlsson
Klingis Entertainment
Games with silly humor
String concatenate (addition).

This topic is closed to new replies.

Advertisement