Click here to Skip to main content
15,909,605 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a text file where I am commenting the lines from the first occurence of the string "OPEN" in line till a dot (.) is occured.
ie OPEN abc def
ghi jkl
mno pqr.

is replaced as below in another file

//OPEN abc def
// ghi jkl
// mno pqr.


But the challenge I am facing is, that I have to "ignore" commenting the lines if the string OPEN is between EXEC and END-EXEC.

ie EXEC SQL
OPEN CUR1
END-EXEC.

My program comments the above 2 lines also( One which starts with OPEN and END-EXEC.)

Is there a way to get the previous line of a file after a string is encountered..?


Thanks in advance,

Faez
Posted
Comments
Resmi Anna 27-Mar-12 6:09am    
Why dont you post the code you have done so far?so that someone can just add 2 or 3 lines of code to that to resolve your issue.
Faez Shingeri 27-Mar-12 6:18am    
<pre>
if (strncmp(buffer2,"OPEN", 4) == 0)
{
found=1;
sprintf(buffer1," // %s",buffer);
fputs(buffer1,fp2);
n=1;
}
else if (strncmp(buffer2,"EXEC",4)== 0)
{
found =1;

sprintf(buffer1," %s",buffer);
fputs(buffer1,fp2);

o=1;

}
</pre>
and then if found =1 , am running a loop till a dot is encountered.

1 solution

You may build a state machine:
Its normal state could be, for instance, RUNNING.
While in RUNNING state, if it matches 'OPEN' then it goes into COMMENT state and start commenting until it matches '.' (going into RUNNING state again). On the other hand, if, while in RUNNING state it matches 'EXEC' then it goes into NOCOMMENT (or whatever) state, thus disabling comments until 'END-EXEC' is found and state becomes back RUNNING.


[update]
(warning: not tested, that is up to you)
C++
enum 
{
  eRUNNING,
  eCOMMENT,
  eNOCOMMENT
};
//...
if ( state == eRUNNING)
{
  if (strncmp(buffer2,"OPEN", 4) == 0)
  {
    fprintf(fp2, "      // %s", buffer);
    state = eCOMMENT;
  }
  else
  {
    fprintf(fp2, "     %s", buffer);
    if (strncmp(buffer2,"EXEC",4)== 0)
    {
      state = eNOCOMMENT;
    }
  }
}
else if (state == eCOMMENT)
{
  fprintf(fp2, "      // %s", buffer);
  if (strncmp(buffer2,".", 1) == 0)
  {
    state = eRUNNING;
  }
}
else
{ // state here is eNOCOMMENT
  fprintf(fp2, "     %s", buffer);
  if (strncmp(buffer2,"END-EXEC",8)== 0)
  {
    state = eRUNNING
  }
}
//...


[/update]
 
Share this answer
 
v3
Comments
Faez Shingeri 27-Mar-12 6:14am    
This ain't working completely.. :-/ The lines of EXEC and END-EXEC are being uncommented with your logic, but not the line with OPEN...
CPallini 27-Mar-12 6:44am    
Did you try it (did you correctly implement it?)?
In my opinion, you didn't get it.
Faez Shingeri 27-Mar-12 7:02am    
Yes... Below is the code..
<pre>
if (strncmp(buffer2,"OPEN", 4) == 0)
{
found=1;
sprintf(buffer1," // %s",buffer);
fputs(buffer1,fp2);
n=1;
}
else if (strncmp(buffer2,"EXEC",4)== 0)
{
found =1;

sprintf(buffer1," %s",buffer);
fputs(buffer1,fp2);

o=1;

}
</pre>
and then if found =1 , am running a loop till a dot is encountered.
CPallini 27-Mar-12 7:50am    
OK. Now I am sure. You didn't get it.
Do you know how a state machine work?
Faez Shingeri 27-Mar-12 7:58am    
Not exactly.. :( Had learned about it long bak in college...

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