#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;
}