删除文件中的注释(C语言)

【题目描述】删除文件中的注释:将C语言源程序(hello.c)文件中的所有注释去掉后存入另一个文件(new_hello.c)。试编写相应程序。

【代码】

c 复制代码
#include <stdio.h>
#include <stdlib.h>
int main(void) {
    FILE *fp1, *fp2;
    if ((fp1=fopen("hello.c", "r")) == NULL) {
        printf("Can't open file");
        exit(0);}
    if ((fp2=fopen("new_hello.c", "w")) == NULL) {
        printf("Can't open file");
        exit(0);}

    int i = 0, flg = 0;
    char str[1000];
    while (!feof(fp1)) {
        char ch = fgetc(fp1);
        if (ch == EOF) {
            break;}
        str[i++] = ch;}
    str[i] = '\0';
    for (int j=0; j<i; j++) {
        if (j<i-1 && str[j]=='/' && str[j+1]=='*') {
            flg = 1;}
        if (flg == 0) {
            fprintf(fp2, "%c", str[j]);}
        if (flg==1 && j<i-1 && str[j]=='*' && str[j+1]=='/') {
            flg = 0;
            j++;}}
     fclose(fp1);
     fclose(fp2);
    return 0;}

暂时没有想到好的方法,生气!!!

相关推荐
RuoZoe11 小时前
重塑WPF辉煌?基于DirectX 12的现代.NET UI框架Jalium
c语言
祈安_4 天前
C语言内存函数
c语言·后端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
czy87874755 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
m0_531237175 天前
C语言-数组练习进阶
c语言·开发语言·算法
Z9fish6 天前
sse哈工大C语言编程练习23
c语言·数据结构·算法
代码无bug抓狂人6 天前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
CodeJourney_J6 天前
从“Hello World“ 开始 C++
c语言·c++·学习
枫叶丹46 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
with-the-flow6 天前
从数学底层的底层原理来讲 random 的函数是怎么实现的
c语言·python·算法