This code will copy the ENTIRE contents of a file. From here, it is a simple matter to copy only a given number of lines.
Code:
#include <stdio.h>
static void MyCopyFile(char *FromFile, char *ToFile);
int main(int argc, char* argv[])
{
MyCopyFile("from.txt", "to.txt");
return 0;
}
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)
{
fputs(FileBuf, to);
}
fclose(to);
}
else
printf("Cannot open %s\n", ToFile);
fclose(from);
}
else
{
printf("Cannot open %s\n", FromFile);
}
}