/* read a line from in-file */
char str[512];
char text[512];
string line;
int status = 0; // 0:normal 1: //... 2: /*... 3: "..." 4: '...'(case '"':)
while(!in.eof())
{
memset(text, 0, sizeof(text));
int i_text = 0;
in.getline(str, sizeof(str));
if(status == 1)
{
status = 0;
}
for(int i=0; i
{
switch (str[i])
{
case '\"': // string like this : puts("it is not comment // here!");
{
if(status == 0)
status = 3;
else if(status == 3)
status = 0;
if(status != 1 && status != 2)
text[i_text++] = str[i];
}
break;
case '\\': //string like this : puts("the string is not end \\\" here \" ");
{
if(status == 3 || status == 4) // in a string
{
if(str[i+1] == '\\' || str[i+1] == '\"' || str[i+1] == '\'')
{
// still in a string, the string not end, status would not be changed!
text[i_text++] = str[i]; // char <\>
text[i_text++] = str[i+1]; // char <\> or <">
++i; // skip next char
}
else
{
text[i_text++] = str[i];
}
}
else if (status != 1 && status != 2)
{
text[i_text++] = str[i];
}
}
break;
case '\'': // string like this : case '"';
{
if(status == 0)
status = 4;
else if(status == 4)
status = 0;
if(status != 1 && status != 2)
text[i_text++] = str[i];
}
break;
case '/':
{
if(str[i+1] == '/' && status == 0)
{
status = 1;
++i;// skip next char
}
else if(str[i+1] == '*' && status == 0)
{
status = 2;
++i;
}
else if(status != 1 && status != 2)
{
text[i_text++] = str[i];
}
}
break;
case '*':
{
if(str[i+1] == '/' && status == 2)
{
status = 0;
++i;
}
else if(status != 1 && status != 2)
{
text[i_text++] = str[i];
}
}
break;
default:
if(status != 1 && status != 2)
text[i_text++] = str[i];
break;
}
}
if(keep_line == 0)
{
int k=0;
for (k=0; k
{
if(isgraph(text[k]))
{
break;
}
}
if(k < strlen(text))
out<
}
else
out<
}
cout<<"+new file: "< "<
return 0;
}
//----------------------------------------------------------------------------------------------------------------------
test
wang@ubuntu:~/桌面$ cat test.c
#include
// test comdel.
int main(void)
{
/* this is single-line comment */
/*
this is muilt-line comment
*/
/*
// comment */ int i =10;
printf("hello world!!//nihao /*gooddss*/\n");/* comment */
/* comment */puts("the string is not end\\\\\" here \"");
}
wang@ubuntu:~/桌面$ comdel test.c
+new file: test.c ---> test.c_
wang@ubuntu:~/桌面$ cat comdel test.c_
cat: comdel: 没有该文件或目录
#include
int main(void)
{
int i =10;
printf("hello world!!//nihao /*gooddss*/\n");
puts("the string is not end\\\\\" here \"");
}