2017上半年课程设计3

本文介绍了一个使用C语言编写的简单程序,该程序能够批量为指定范围内的代码行添加注释,并展示如何读取并打印修改后的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
void formatPrograme(char *sourcefile, char *targetfile);
void outprogram(char *filename);
int main( )
{
    formatPrograme("source.c", "target.c");
    outprogram("target.c");
    printf("任务完成!\n");
    return 0;
}
void formatPrograme(char *sourcefile, char *targetfile)
{
    int m, n;
    char line[256];
    //将文件中的数据读入到字符数组中
    FILE *fpin = fopen(sourcefile,"r");  //以输入的方式打开文件
    if(fpin==NULL)       //测试是否成功打开
    {
        printf("source code read error!\n");
        exit(1);
    }
    FILE *fpout = fopen(targetfile,"w");  //以输入的方式打开文件
    if(fpout==NULL)       //测试是否成功打开
    {
        printf("source code write error!\n");
        exit(1);
    }

    printf("您要将第m行开始的n行代码作为注释,请输入m和n:");
    scanf("%d %d", &m, &n);
    int n1=0;
    while(!feof(fpin))
    {
        fgets(line,256,fpin);
        n1++;
        if(n1>=m&&n1<m+n)
            fputs("//",fpout); //指定行内,本行始加注释
        fputs(line, fpout);
    }
    fclose(fpout);
    fclose(fpin);
}
void outprogram(char *filename)
{
    //建立一个字符数组存放文件的数据
    char line[256];
    //行数记录
    int n=1;
       //将文件中的数据读入到字符数组中
    FILE *fp=fopen(filename,"r");  //以输入的方式打开文件
    if(fp==NULL)       //测试是否成功打开
    {
        printf("source code read error!\n");
        exit(1);
    }
    while(!feof(fp))
    {
        fgets(line,256, fp);  //读入一行
        printf("%d    %s\n", n, line);
        n++;
    }
    fclose(fp);
    return;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值