但对于/*
*/还不能实现。
程序中有两点错误:
1,字符'\'要写成转义符
2,while条件不正确
对程序修改如下:
#include
#include
#include
#include
void
main()
{
char
ch,ch3
char
ch1
char
ch2
FILE
*r
FILE
*w
r=fopen("f:\\a.txt","r")
w=fopen("f:\\b.txt","w")
ch1=fgetc(r)
while(ch1!=EOF)
{
ch2=fgetc(r)
if(ch1=='\\'&&ch2=='\\')
//这里------
{
do
{
ch3=fgetc(r)
}while(ch3!='\n')
//这里------
}
else
{
fputc(ch1,w)
fputc(ch2,w)
}
ch=fgetc(r)
ch1=ch
}
}
#include <stdio.h>int main()
{
FILE *fp,*fp1
char str[99]=""
int i=0
fp=fopen("test.c","r") //要打开的源文件
fp1=fopen("new.c","w") //另存为
while(fgets(str,99,fp)!=NULL)
{
for(i=0i<99i++)
if(str[i]=='/'&&str[i-1]=='/'){str[i-1]='\n'str[i]='\0'break}
fputs(str,fp1)
}
fcloseall()
return 0
} 如果有/* */块注释的话,用这个
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp,*fp1
char str[99]="",str1[99]=""
int i,j,no=0
fp=fopen("test.c","r") //要打开的源文件
fp1=fopen("new.c","w") //另存为
while(fgets(str,99,fp)!=NULL)
{
for(i=0i<99i++)
{
if(str[i]=='/'&&str[i-1]=='/')
{str[i-1]='\n'str[i]='\0'}
if(str[i]=='*'&&str[i-1]=='/')
{str[i-1]='\0'no=1fputs(str,fp1)}
if(str[i]=='/'&&str[i-1]=='*')
{
for(j=0j<98-ij++){str[j]=str[i+j+1]}
str[j]='\0'
no=0
}
}
if(no==0)fputs(str,fp1)
}
fcloseall()
return 0
}
给个思路吧:按行读取文件,在本行寻找字符“//”和“/*”
若找到“//”,则从字符/开始一直到行尾的所有字符全都去掉
若找到“/*”,则从此处开始寻找“*/”,删除字符“/*”和“*/”及二者之间的字符