Writing beyond that is rampaging through stack space, overwriting every local variable behind it until it finds and corrupts your stack frame itself, crashing your program the next time it tries to do anything to the stack.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
char theCommand[16384] = "cat ";
for (i = 1; i < argc; i++)
{
strcat(theCommand, argv);
}
strcat(theCommand, " >> testfile.txt");
system(theCommand);
return 0;
}
D'oh! That would explain the segmentation fault, then.
Thank you very much for your help!
I had to put some white space in between the files because it was concatenating them together like: file.txtfile2.txtfile3.txt, is this the acceptable way of doing it (below)?
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
char theCommand[16384] = "cat ";
for (i = 1; i < argc; i++)
{
strcat(theCommand, argv);
strcat(theCommand, " ");
}
strcat(theCommand, " >> testfile.txt");
system(theCommand);
return 0;
}
usage of strncat is the same as strcat. The only difference is that strncat has an aditional third argument, that you use to tell the function the maximum amount of characters that should be copied to the destination buffer. This is very important to prevent your programs being vulnerable to buffer overflow errors.
for (i = 1; i < argc; i++)
{
strncat(theCommand, argv, sizeof(theCommand) - 1);
}