将一个C 语言源程序文件中所有注释去掉后,存入另一个文件。 20
4个回答
展开全部
我以前写了一个,功能都实现了,但就是执行后,程序格式有点不够整齐,所以只可以给你参考
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100int mode = 0;
//char *deal(char *buf,)
void delete(int argc,char **argv) {
if(2 != argc) {
printf("usage:%s file\n",argv[0]);
exit(-1);
}
FILE *fp0 = fopen(argv[1],"r+");
if(NULL == fp0) {
perror("fopen file");
exit(-1);
}
FILE *fp1 = tmpfile();
if(NULL == fp1) {
perror("fopen temp");
exit(-1);
}
int i=0;
char c,buf[3];
while((c=fgetc(fp0)) != EOF) {
buf[i] = c;
switch(mode) {
case 0:
if('/' == c) {
mode = 1;
} else if('"' == c) {
mode = 2;
} else {
fputc((int)c,fp1);
}
break;
case 1:
if('/' == c) {
mode = 4;
}
else if('*' == c) {
mode = 5;
}
else {
fputc('/',fp1);
mode = 0;
if('"' == c) {
mode = 2;
}
else {
fputc((int)c,fp1);
}
}
break;
case 2:
if(39 == buf[i]&&39 == buf[(i+1)%3]) {
mode = 0;
fputc('"',fp1);
fputc(39,fp1);
}
else {
fputc('"',fp1);
fputc((int)c,fp1);
mode = 3;
if('"' == c) {
mode = 0;
}
}
break;
case 3:
fputc((int)c,fp1);
if('"' == c) {
mode = 0;
}
break;
case 4:
if('\n' == c) {
mode = 0;
}
break;
case 5:
if('*' == c) {
mode = 6;
}
break;
case 6:
printf("%c",c);
if('/' == c) {
mode = 0;
} else {
mode = 5;
}
break;
default:
printf("program wrong!\n");
exit(-1);
}
i++;
i%=3;
}
fclose(fp0);
fp0 = fopen(argv[1],"w+");
if(NULL == fp0) {
perror("fopen file");
exit(-1);
}
rewind(fp1);
char a[SIZE];
memset(a,0,sizeof(a));
while(i = fread(a,1,99,fp1)) {
fwrite(a,1,i,fp0);
}
fclose(fp0); fclose(fp1);
}
int main(int argc,char **argv) { delete(argc,argv);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100int mode = 0;
//char *deal(char *buf,)
void delete(int argc,char **argv) {
if(2 != argc) {
printf("usage:%s file\n",argv[0]);
exit(-1);
}
FILE *fp0 = fopen(argv[1],"r+");
if(NULL == fp0) {
perror("fopen file");
exit(-1);
}
FILE *fp1 = tmpfile();
if(NULL == fp1) {
perror("fopen temp");
exit(-1);
}
int i=0;
char c,buf[3];
while((c=fgetc(fp0)) != EOF) {
buf[i] = c;
switch(mode) {
case 0:
if('/' == c) {
mode = 1;
} else if('"' == c) {
mode = 2;
} else {
fputc((int)c,fp1);
}
break;
case 1:
if('/' == c) {
mode = 4;
}
else if('*' == c) {
mode = 5;
}
else {
fputc('/',fp1);
mode = 0;
if('"' == c) {
mode = 2;
}
else {
fputc((int)c,fp1);
}
}
break;
case 2:
if(39 == buf[i]&&39 == buf[(i+1)%3]) {
mode = 0;
fputc('"',fp1);
fputc(39,fp1);
}
else {
fputc('"',fp1);
fputc((int)c,fp1);
mode = 3;
if('"' == c) {
mode = 0;
}
}
break;
case 3:
fputc((int)c,fp1);
if('"' == c) {
mode = 0;
}
break;
case 4:
if('\n' == c) {
mode = 0;
}
break;
case 5:
if('*' == c) {
mode = 6;
}
break;
case 6:
printf("%c",c);
if('/' == c) {
mode = 0;
} else {
mode = 5;
}
break;
default:
printf("program wrong!\n");
exit(-1);
}
i++;
i%=3;
}
fclose(fp0);
fp0 = fopen(argv[1],"w+");
if(NULL == fp0) {
perror("fopen file");
exit(-1);
}
rewind(fp1);
char a[SIZE];
memset(a,0,sizeof(a));
while(i = fread(a,1,99,fp1)) {
fwrite(a,1,i,fp0);
}
fclose(fp0); fclose(fp1);
}
int main(int argc,char **argv) { delete(argc,argv);
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
比对// 跟 /* */
读源程序,建新文件,比对之后存入新文件,//之后的内容不存,直到换行,/*之后的不存 直到*/
读源程序,建新文件,比对之后存入新文件,//之后的内容不存,直到换行,/*之后的不存 直到*/
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
(⊙_⊙)?
你的意思是用程序删掉?还是手动删掉?
你的意思是用程序删掉?还是手动删掉?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
int main()
{
FILE *fp1, *fp2;
char num[1000];
int i = 0;
if ((fp1 = fopen("text1.txt", "r+")) == NULL)
printf("wrong - ---- text1.txt");
if ((fp2 = fopen("text2.txt", "r+")) == NULL)
printf("wrong _____text2.txt");
for (i = 0; i < 1000; i++)
{
num[i] = fgetc(fp1);
}
i = 0;
while (i!= 1000)
{
if(num[i] == '/')
if (num[i + 1] == '/')
{
num[i] = NULL;
num[i + 1] = NULL;
}
if(num[i] == '*')
if (num[i + 1] == '/')
{
num[i] = NULL;
num[i + 1] = NULL;
}
if(num[i] == '/')
if (num[i + 1] == '*')
{
num[i] = NULL;
num[i + 1] = NULL;
}
i++;
}
for (i = 0; i < 1000; i++)
fputc(num[i], fp2);
//fprintf(fp2, "%s", num);
fclose(fp1);
fclose(fp2);
return 0;
}
int main()
{
FILE *fp1, *fp2;
char num[1000];
int i = 0;
if ((fp1 = fopen("text1.txt", "r+")) == NULL)
printf("wrong - ---- text1.txt");
if ((fp2 = fopen("text2.txt", "r+")) == NULL)
printf("wrong _____text2.txt");
for (i = 0; i < 1000; i++)
{
num[i] = fgetc(fp1);
}
i = 0;
while (i!= 1000)
{
if(num[i] == '/')
if (num[i + 1] == '/')
{
num[i] = NULL;
num[i + 1] = NULL;
}
if(num[i] == '*')
if (num[i + 1] == '/')
{
num[i] = NULL;
num[i + 1] = NULL;
}
if(num[i] == '/')
if (num[i + 1] == '*')
{
num[i] = NULL;
num[i + 1] = NULL;
}
i++;
}
for (i = 0; i < 1000; i++)
fputc(num[i], fp2);
//fprintf(fp2, "%s", num);
fclose(fp1);
fclose(fp2);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询