473,785 Members | 3,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete comments in .c file

I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?

Please show me example code.

Nov 13 '05
39 16799
Jeremy Yallop wrote:

Tim Hagan wrote:
This will replace each /* ... */ style comment with a single space:

#include<stdio. h>
int main(void){i\
nt c,p=-1,k=0,s=0
;while((c=getch ar
())!=EOF){if(s= =0
){if(p=='/'&&c==
'*'){s=1;k=2;}e l\
se if(c=='\"'&&p
!='\\'&&p!='\'' )s
=2;}else if(s==1)
{if (p=='*'&&c==
'/')s=0;}else if(
s==2){if(c=='\" '
&&p!='\\')s=0;} if
(k==1)putchar(' '
);if(p>0&&s!=1) {
if(!k)putchar(p );
if(--k<0)k=0;}p=c
;}putchar(p);re t\
urn 0;}


It doesn't handle line-splicing. Also, putchar(-1) is not portable.


putchar(-1) is never executed in the above code, but you're right
about the line-splicing. Oh, well, back to the drawing board ...

--
Tim Hagan
Nov 13 '05 #11
>> I want to delete all comments in .c file.

Size of .c file is very big.


You work for SCO's Linux division, don't you?


<GRIN>

Nov 13 '05 #12
Tim Hagan wrote:
Jeremy Yallop wrote:

Tim Hagan wrote:
> This will replace each /* ... */ style comment with a single space:
>
> #include<stdio. h>
> int main(void){i\
> nt c,p=-1,k=0,s=0
> ;while((c=getch ar
> ())!=EOF){if(s= =0
> ){if(p=='/'&&c==
> '*'){s=1;k=2;}e l\
> se if(c=='\"'&&p
> !='\\'&&p!='\'' )s
>=2;}else if(s==1)
> {if (p=='*'&&c==
> '/')s=0;}else if(
> s==2){if(c=='\" '
> &&p!='\\')s=0;} if
> (k==1)putchar(' '
> );if(p>0&&s!=1) {
> if(!k)putchar(p );
> if(--k<0)k=0;}p=c
> ;}putchar(p);re t\
> urn 0;}


It doesn't handle line-splicing. Also, putchar(-1) is not portable.


putchar(-1) is never executed in the above code


putchar(-1) is executed if EOF is encountered immediately.

Jeremy.
Nov 13 '05 #13
Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> spoke thus:
putchar(-1) is executed if EOF is encountered immediately.


Why would one want to write putchar(-1) anyway...?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #14
"Timex" <su********@hot mail.com> wrote in
news:bn******** **@news.kreonet .re.kr:
I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?

Please show me example code.


Just grab an evaluation copy of Codewright from Borland. Do a
search-replace on . <-- regexp and restrict "to comments" with a
replacement string of nothing. I just did it to a very large file in about
5 seconds. Now what does this have to do with the C language? The C
language does not specify how to delete comments.

--
- Mark ->
--
Nov 13 '05 #15
Jeremy Yallop wrote:

Tim Hagan wrote:
Jeremy Yallop wrote:

Tim Hagan wrote:
> This will replace each /* ... */ style comment with a single space:
>
> #include<stdio. h>
> int main(void){i\
> nt c,p=-1,k=0,s=0
> ;while((c=getch ar
> ())!=EOF){if(s= =0
> ){if(p=='/'&&c==
> '*'){s=1;k=2;}e l\
> se if(c=='\"'&&p
> !='\\'&&p!='\'' )s
>=2;}else if(s==1)
> {if (p=='*'&&c==
> '/')s=0;}else if(
> s==2){if(c=='\" '
> &&p!='\\')s=0;} if
> (k==1)putchar(' '
> );if(p>0&&s!=1) {
> if(!k)putchar(p );
> if(--k<0)k=0;}p=c
> ;}putchar(p);re t\
> urn 0;}

It doesn't handle line-splicing. Also, putchar(-1) is not portable.
putchar(-1) is never executed in the above code


.... unless one tries to remove the comments from an empty file. :-)
putchar(-1) is executed if EOF is encountered immediately.


So just insert 'if (p > 0)' before the final putchar.

--
Tim Hagan
Nov 13 '05 #16
Mark A. Odell wrote:
Now what does this have to do with the C language? The C
language does not specify how to delete comments.


The Standard says: "Each comment is replaced by one space character." If
that doesn't specify how to delete comments, I don't know what does.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #17
On Fri, 31 Oct 2003 22:47:18 +0000 (UTC), in comp.lang.c , Richard
Heathfield <do******@addre ss.co.uk.invali d> wrote:
Mark A. Odell wrote:
Now what does this have to do with the C language? The C
language does not specify how to delete comments.


The Standard says: "Each comment is replaced by one space character." If
that doesn't specify how to delete comments, I don't know what does.


I guess Mark's point is that to be sure of being syntactically
identical to the original you should replace all comments by a space.
For instance
int i = 23/* */12;
should still generate a syntax error . :-)

What the OP would expect it to do with
double i = 32 //* */ 4
;
is anyone's guess. I guess you'd have to have a C99 and a C89 mode.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #18
Here's a perl script which will handle *MOST* sane C code...

Some things that it will miss (scan manually for it first:

a double quote inside of single quotes (e.g.)
char confusion = '"';

C-99 // comments like this

I'm sure that some people can come up with other convoluted counter-examples.

It reads and plays with the entire file, so it will need to hold
at least two or three copies of it in RAM. (for today's computers,
that would be some number of megabytes).

If you want any of the above fixed, feel free to send me a cheque.
_______________ _______________ _______________ _______
#!/usr/bin/perl
$s=join("",<>);
# printf "[[%s]]\n\n",$s;
$s=~ s/("(\\\\|\\"|[^"])*")|(\/\*([^*]|\*(?=[^\/]))*\*\/)|(\/\/.*)/[[$1 ]]/g;
printf "[[%s]]\n\n",$s;
_______________ _______________ _______________ _______
Yep, That's it... 5 lines including the shell header.

Timex wrote:
I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?

Please show me example code.

--
Stephen Samuel +1(604)876-0426 sa****@bcgreen. com
http://www.bcgreen.com/~samuel/
Powerful committed communication. Transformation touching
the jewel within each person and bringing it to light.
Nov 13 '05 #19
Stephen Samuel <st************ @telus.net> wrote:
Here's a perl script which will handle *MOST* sane C code...

<snip>

Since when is perl topical in c.l.c?

BTW:
Does your "solution" account for comment delimiters inside string
literals? (I'm unfortunately unable to decrypt the line-noise
provided.)

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8807
by: Ryan | last post by:
I have a table in my database on SQL Server which holds a file name that refers to a file that is stored on the server. I would like to create a trigger to delete this file from the server if the row in the table is deleted. I have been trying to use this command in a trigger (<filename> is the name and path of the file): xp_cmdshell "delete <filename>" If some one could please help I would appreciate it very much. I would love a...
0
2104
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape. Once the compare is complete the file that was copied to a temp location needs to be deleted. I am using the method file.copy(sourcePath, tempPath, true) to copy the file and then file.delete(tempPath) to delete the file. On some of the files...
3
1496
by: Huahe | last post by:
I try to delete a file in a for each loop. My code checks if the file exists and if it does, it will delete the file and create a new file with the same name. The first time it works perfect, but the second time it gives me a FileIOException. I want to prevent this from happening. What can i do to make sure the file isn't in use anymore the second time i try to delete it? for each dr in table.rows if file.exist("C:\sample.txt") then...
23
8949
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use the remove function that is included with <cstdio> but cannot get it to work properly. My reference book has the following....
3
2932
by: News | last post by:
Is it possible to delete a file by copying it to the "bit bucket" or "null device"? Back in my youth when I live in VMS-land you could delete a file by copying it to NL: ========== I have written a windows service as part of an interface between two different systems. The first system will write output into a file in a
1
1908
by: Matt Hamilton | last post by:
I have a simple image gallery where I want to allow users to delete files. The problem I have is that after an image is displayed in the browser, I am not able to delete the file because "The process cannot access the file ... It is being used by another process". I also get this error when trying to delete through explorer on the server. I can delete the file if I stop the Web Server service... Is there a way around this? Here is the...
2
4750
by: createdbyx | last post by:
I am trying to make a file sync utillity to sync files between my laptop and my desktop pc. On my desktop machine (xp pro sp2) I have shared my "Visual Studio Projects" folder using windows simple file sharing. And have specified the "Allow network users to change my files." option as well. Then on my laptop (xp home sp2) I have mapped a network drive using windows explorer. So on my laptop I have a Y: drive that points to the "Visual...
0
2851
by: smanisankar | last post by:
hi, the following is the full page code for uploading a file to server. since i got no idea to overwrite the file, i want delete the file if the file is already uploaded. i got the folder name and filename of the file to delete from the request.QueryString("path") so i got the above error when i try to delete the file before upload. Please anyone help me to solve out from this error. <%@ Import Namespace="System.IO" %>
5
1471
by: Neven Klofuar | last post by:
hi, I have a problem when trying to delete a file. I have to extract some information from a file, and then I have to delete it. When I try to delete it after I read it, I get a "Access denied" error. help pls, Neven ********************************
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.