//删除c语言程序中所有的注释语句,要正确处理带引号的字符串与字符串常量
#include <stdio.h>
using namespace std;
#define MAXLINE 1000
void rcomment(int c); void in_comment(void); void deleteTail(void); FILE* fp; FILE* fp2;
int main() { fp=fopen("C:\\Users\\Administrator\\Desktop\\leetcode.cpp","r"); fp2=fopen("C:\\Users\\Administrator\\Desktop\\out.txt","w"); bool deleteMutiline=false; char line[MAXLINE]; int c; while((c=getc(fp))!=EOF) rcomment(c); return 0; } void rcomment(int c){ int d; if(c=='/'){ if((d=getc(fp))=='*') in_comment(); else if (d=='/') deleteTail(); else { putc(c,fp2); putc(d,fp2); } }else putc(c,fp2); } void in_comment(void){ int c,d; c=getc(fp); d=getc(fp); while (c!='*'||d!='/') { c=d; d=getc(fp); } } void deleteTail(){ int c; c=getc(fp); while (c!='\n') { c=getc(fp); } putc('\n',fp2); }

涉及标准库中的文件操作函数getc和putc,从文件中读单个字符,写单个字符到文件中。

以及文件结构FILE。

具体思路:

相关文章:

  • 2021-04-05
  • 2021-11-24
  • 2021-07-06
  • 2021-05-23
  • 2022-12-23
  • 2021-12-12
  • 2021-12-16
  • 2022-12-23
猜你喜欢
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2021-09-23
  • 2022-12-23
相关资源
相似解决方案