如何忽略C中的特定字符串操作 [英] How to ignore a particular string operation in C

查看:91
本文介绍了如何忽略C中的特定字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个文本文件,在其中注释行中从字符串"OPEN"的第一次出现到出现点(.)的行.
即OPEN abc def
ghi jkl
mno pqr.

在另一个文件中被替换为以下内容

//打开abc def
//ghi jkl
//mno pqr.


但是我面临的挑战是,如果字符串OPEN位于EXEC和END-EXEC之间,我必须忽略"注释行.

即EXEC SQL
OPEN CUR1
结束执行.

我的程序也注释了以上两行(以OPEN和END-EXEC开头的一行.)


是否有办法在遇到字符串后获取文件的前一行..


预先感谢,

Faez

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

推荐答案

您可以构建状态机:
它的正常状态可以是例如RUNNING.
RUNNING状态下,如果它与" OPEN "匹配,则进入COMMENT状态并开始注释,直到它与"."匹配(继续再次进入RUNNING状态).另一方面,如果在RUNNING状态下与" EXEC "匹配,则进入NOCOMMENT(或其他状态),从而禁用注释,直到" END"为止-EXEC ''已找到,状态恢复为RUNNING.


[更新]
(警告:未经测试,取决于您)
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)
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]



[/update]


这篇关于如何忽略C中的特定字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆