This may repeat some of what
kbw has said - but I started it this
morning before leaving for the day job.
Well, if you analyse these two lines - you can see that
strcat and
sprintf are different:
1 2 3
|
strcat(concatenate_str," > tmp00");
sprintf(sprint_str,"%s > tmp01", sprint_str);
|
Also you will notice that the result of the
sprintf statement did not go to the standard output (screen) where as the reslt of a
printf does.
printf,
sprintf and
fprintf all BUILD a result string in the same way - you can have embbeded controls like
%s, etc.
printf output goes to standard output,
sprintf output goes to a buffer, and
fprintf goes to an output stream (file).
strcpy takes two pointers to a buffer and simply copies the chars (including the terminating 0) from the source buffer to the destination buffer. There is no building up of the string if you see what I mean - just a straight copy (overwriting anything that is already present in the destination buffer).
strcat.
takes a pointer to a destination buffer and a pointer to a source buffer. It finds the terminationg 0 of the destination buffer and starting from there tags on the data from the source buffer and adds a terminating 0.
there is no building up of the string like
printf
sprintf, strcpy, strcat (and also strlen function) are all considered dangerous - the all use pointer to buffers -
there are no checks to see if the destination buffer is large enough to hold the resulting string -
so can easily lead to buffer overflow.
Note you can use c++ sytle strings instead of c style strings - but the same problems can happen.
A fair few of the c++ style string operators take
char *:
for example:
1 2 3 4 5 6
|
#include <string>
std::string s;
char *pc;
s += pc; //can be a problem if pc is uninitialized
|