I have this code as below for copying the contents of one file to another file. What I want to know is Can I use this code as below to copy the first line of one file and place it in another file, then take the first line of the second file and copy it to "ToFile" so that the data will be stored beside each other. The program must then take the 2nd line for each or the two files and store them on the next line of "ToFile" but beside each other and so on until all the data has been copied from the files.
static void
MyCopyFile(char *FromFile, char *ToFile)
{
FILE *from;
FILE *to;
char FileBuf[256];
if ((from = fopen(FromFile, "r")) != NULL)
{
/* THE "a" WILL APPEND TO THE FILE, IF YOU WANT TO CREATE A NEW FILE, USE "w" */
if ((to = fopen(ToFile, "a")) != NULL)
{
while (fgets(FileBuf, 255, from) != NULL){
char *p = strchr( FileBuf, '\n' );
if ( p ) *p = '\0'; // this removes the newline
strcat( FileBuf, " X X X\n\n" );
fputs(FileBuf, to);
}
fclose(to);
}
else
printf("Cannot open %s\n", ToFile);
fclose(from);
}
else
{
printf("Cannot open %s\n", FromFile);
}
}