Here is some basic code that works without the necessity of copying buffers all over the place. I am sure you can easily adapt it to your needs. I have had to guess at the actual content of your input file so I have added the file contents below also.
FILE *fpIn, *fpOut;
char pfst[10][50];
char pfet[10][50];
char buffer[MAX_LEN_SINGLE_LINE];
int length;
char* tok;
int found = 0;
fpIn = fopen("Test.txt", "r");
fpOut = fopen("PerformSectionExtract.txt","w");
while(fgets(buffer, MAX_LEN_SINGLE_LINE, fpIn))
{
if(strncmp(buffer,"PERFORM",7) == 0 )
{
fputs(" ", fpOut);
fputs(buffer, fpOut);
tok = strtok(buffer + 8, ".\n");
if (tok != NULL)
{
sprintf(pfst[0], "%s SECTION", tok);
sprintf(pfet[0], "%s -EXIT", tok);
}
continue;
}
length = strlen(pfst[0]);
if(strncmp(buffer, pfst[0], length ) == 0 )
{
found = 1;
}
if (found == 1)
{
fputs(" ", fpOut);
fputs(buffer, fpOut);
if(strstr(buffer, pfet[0]) != NULL)
{
found = 0;
}
}
}
Test.txt
PERFORM test1
blah blah blah
test1 SECTION
test1 1
test1 2
test1 3
test1 -EXIT
ignored stuff 1
ignored stuff 2
ignored stuff 3
PERFORM nextTest
more ignored stuff 1
more ignored stuff 2
more ignored stuff 3
nextTest SECTION
nextTest 1
nextTest 2
nextTest 3
nextTest -EXIT
PerformSectionExtract.txt
PERFORM test1
test1 SECTION
test1 1
test1 2
test1 3
test1 -EXIT
PERFORM nextTest
nextTest SECTION
nextTest 1
nextTest 2
nextTest 3
nextTest -EXIT