Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am trying to extract a range of text from an input file "Test.txt"
The range starts from the occurrence of a string stored in pfst[0] till the string stored in pfet[0]. The final o/p is stored in the file "PerformSectionExtract.txt".

The string values prints fine before the parsing, but NULL values are printed after entering the parsing mechanism. Below is my code :
C
char pfst[1500][50];
char pfet[1500][50];

char fileOrig[MAX_PATH] = "Test.txt";
char fileRepl2[MAX_PATH] = "PerformSectionExtract.txt";

FILE *fp1, *fp2, *fp3, *fp4;
fp1 = fopen(fileOrig,"r+");
fp4 = fopen(fileRepl2,"w+");



while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
		{
strcpy(buffer2,buffer);
length= strlen(pfst[0])-1;

if(strncmp(buffer2, pfst[0], length ) == 0 )
	{
	 found2=1;
	}
 if(found2 ==1)
	{
        sprintf(buffer4,"       %s",buffer);
        fputs(buffer4,fp4);
	   if(strstr(buffer,pfet[0]) != NULL)
		{
		found2 = 0;
		count2++;
                }
	}
}


The code works fine when I hard code the string as below
C
if(strncmp(buffer2, "string entered here", length ) == 0 )

But, this won't serve the purpose.. :-(

Thanks in advance,
Faez
Posted
Updated 18-Jul-12 21:46pm
v2
Comments
Richard MacCutchan 19-Jul-12 4:37am    
What is the content of pfst[0] and why does pfst need 1500 entries? Stepping through your code with the debugger would likely solve this very quickly.
Faez Shingeri 19-Jul-12 4:49am    
Please refer my reply to Prafulla Vedante.
Thanks.
Richard MacCutchan 19-Jul-12 5:14am    
This does not answer my question: what is the actual content of pfst[0]? Your code looks unnecessarily complicated for what you are trying to do, all these copy and concatenation operations could be simplified.
Also why are you using the array in pfst and pfet when you only ever compare against the content of the first element in each?
Faez Shingeri 19-Jul-12 5:21am    
This variable pfst[][] stores the start range of a section and pfet[][] stores the end range of a section.
For Eg:

This is a test SECTION. //pfst[0] contains this string.
blah blah blah
blah blah blah
blah blah blah
This is a test-EXIT. // pfet[0] containd this string.

Now I want to extract all the contents in the range of pfst[0] and pfet[0]. Similarly pfst[1] & pfet[1] contains another strings.

Hope this answers your query. Do let me know.
Richard MacCutchan 19-Jul-12 5:37am    
You have still not answered my question about the actual values you have stored in pfst[0] and pfet[0] and where those strings occur in your input file. Until you can match your stored keywords with the content of your file this will not work.

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.
C++
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);
//          i++, j++, p++;
        }
        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;
//          count2++;
        }
    }
}

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
 
Share this answer
 
Comments
Faez Shingeri 20-Jul-12 0:53am    
My Test.txt file looks like this,
<pre lang=="c++">
Data to be ignored
Data to be ignored
PERFORM P5X98A-PGM-INITS. /* String after PERFORM to be stored in pfst[0] and appended with SECTION,Similarly pfet[0] apended with -EXIT.*/
Data to be ignored
Data to be ignored

//pfst[0]=P5X98A-PGM-INITS SECTION.

P5X98A-PGM-INITS SECTION.
Data to be stored
Data to be stored
Data to be stored
Data to be stored
Data to be stored
Data to be stored
P5X98A-PGM-INITS-EXIT. //where pfet[0]=P5X98A-PGM-INITS-EXIT

Data to be ignored
Data to be ignored
Data to be ignored
Data to be ignored

//pfst[0]=P5X98A-PGM-INITS SECTION.

P5X98A-PGM-INITS SECTION.
Data to be stored
Data to be stored
Data to be stored
Data to be stored
Data to be stored
Data to be stored
P5X98A-PGM-INITS-EXIT. //where pfet[0]=P5X98A-PGM-INITS-EXIT
Data to be ignored
Data to be ignored
</pre>

And after I run this code, I get all the "data to be ignored" also, till P5X98A-PGM-INITS-EXIT.
Richard MacCutchan 20-Jul-12 4:15am    
I fed the above data into my program and got this result:

PERFORM P5X98A-PGM-INITS. /* String after PERFORM to be stored in pfst[0] and appended with SECTION,Similarly pfet[0] apended with -EXIT.*/
P5X98A-PGM-INITS SECTION.
Data to be stored
Data to be stored
Data to be stored
Data to be stored
Data to be stored
Data to be stored
P5X98A-PGM-INITS-EXIT. //where pfet[0]=P5X98A-PGM-INITS-EXIT
P5X98A-PGM-INITS SECTION.
Data to be stored
Data to be stored
Data to be stored
Data to be stored
Data to be stored
Data to be stored
P5X98A-PGM-INITS-EXIT. //where pfet[0]=P5X98A-PGM-INITS-EXIT
NOTE: I had to make one minor change as the -EXIT string does not have a space before it so the line of code that sets pfet[0] should read:
sprintf(pfet[0], "%s-EXIT", tok);
Faez Shingeri 23-Jul-12 6:41am    
I really don't understand why I ain't getting the same output :-|
I am getting extra two line of "Data to be ignored" at the start of output file.
Also, it stops working if I give a variable to pfst[z] and pfet[z] (instead of pfst[0]) coz there are a lot of different strings on whom the same operations need to be performed.
Richard MacCutchan 23-Jul-12 9:05am    
In both cases my guess would be that you are doing something wrong. Please use the "Improve Question" link in your question above and show the code you are using.
Can you share the code where you have assigned string value to
C++
pfst[0]
and
C++
pfet[0]
 
Share this answer
 
Comments
Faez Shingeri 19-Jul-12 4:46am    
Here you go. I am initially extracting the word after all the PERFORM in the file and then appending "SECTION." to it and storing it in pfst[][] and another append of "-EXIT." to it and storing it in pfet[][]. I now have to extract all the text range between pfst[n] to pfet[n], where n>1000.

<pre>
if(strncmp(buffer2,"PERFORM",7) == 0 )
{

sprintf(buffer1," %s",buffer);
fputs(buffer1,fp2);
strcpy(buffer3, &(buffer2[7]));
tok= strtok (buffer3,".\n");
strcpy(buffer4,buffer3);
if (tok!=NULL)
{
strcat(buffer3," SECTION.");
strcat(buffer4,"-EXIT.");
strcpy(pfst[j++],buffer3);
strcpy(pfet[k++],buffer4);
p++;
}
}
</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900