473,769 Members | 4,470 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 #1
39 16795
> I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?


Assuming you have C comments /* like this, right? */
You can use UNIX sed (stream editor). You're not going to believe this,
but: cat input.c | sed -e 's/\/\*.*\*\///g' > output.c

Do a diff to make sure it's working correctly.

--
Jem Berkes
http://www.sysdesign.ca/
Nov 13 '05 #2


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

Size of .c file is very big.

Any good idea to do this?

Assuming you have C comments /* like this, right? */
You can use UNIX sed (stream editor). You're not going to believe this,
but: cat input.c | sed -e 's/\/\*.*\*\///g' > output.c

Do a diff to make sure it's working correctly.


That would only work if all comments are in the form you describe, which
isn't likely. e.g. it won't work if the comments are:

/* Start of a fairly common form
* of comment block.
*/

or:

/* set x: */ x = 7; /* now more stuff... */

In the first case it won't delete the comment while in the second
unusual case it'll delete both the comments plus the "x = 7;" assignment
between them.

You need to Google around or if you want a UNIX tool solution, post this
to a UNIX NG (e.g. comp.unix.quest ions or comp.unix.shell ).

Ed.

Nov 13 '05 #3
Jem Berkes <je*@users.pc9_ _org> writes:
I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?


Assuming you have C comments /* like this, right? */
You can use UNIX sed (stream editor). You're not going to believe this,
but: cat input.c | sed -e 's/\/\*.*\*\///g' > output.c

Do a diff to make sure it's working correctly.


That won't detect multi-line comments. It also fails to properly
ignore comment delimiters inside string and character literals. It
deletes everything from the first "/*" on a line to the last "*/" on
the same line; for example, it transforms this:
x = /* one comment */ 42; /* another comment */
to this:
x =
And it replaces each comment by nothing rather than by a blank, so the
following valid C fragment:
x = sizeof/*comment*/int;
is replaced with this:
x = sizeofint;

If you're dealing with C99 code you'll have to worry about "//"
comments (many pre-C99 compilers support these as an extension).

Stripping C comments is a lot more complex than it looks; you almost
have to duplicate most of the functionality of the preprocessor to get
it right.

I really have to ask the original poster: why do you want to do this?

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #4
"Timex" <su********@hot mail.com> 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.

tclsh <<':eof'
set c [open something.c]
set b [read $c]
close $c

regsub -all {//[^\n]*\n} $b \n b
regsub -all {/[*]([^*]|[*](?!/))*[*]/} $b {} b

set c [open something-1.c w]
puts $c $b
close $c
:eof

--
Derk Gwen http://derkgwen.250free.com/html/index.html
The whole world's against us.
Nov 13 '05 #5
Derk Gwen <de******@HotPO P.com> writes:
"Timex" <su********@hot mail.com> 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.

tclsh <<':eof'
set c [open something.c]
set b [read $c]
close $c

regsub -all {//[^\n]*\n} $b \n b
regsub -all {/[*]([^*]|[*](?!/))*[*]/} $b {} b

set c [open something-1.c w]
puts $c $b
close $c
:eof


This seems to add an extra blank line at the end of the output file.
It transforms "token/**/pasting" to "tokenpasti ng", which doesn't
violate the original poster's requirements, but it doesn't match the
way comments are treated in C.

It also doesn't ignore comment delimiters in string and character
literals.

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #6
Assuming you have access to the C preprocessor (cpp) you can do a quick hack
to use that. Isolate the file from it's include files and run like,

cpp -nostdinc file.c > new_file.c

It should warn that it cannot find all the include files. Now put your
#includes back in and you should have a comment free file.

This is a quick and dirty solution because the C preprocessor is trying to
do a whole load of stuff aswell as stripping comments. You need to make
sure that there are no #defines because otherwise they will be expanded out.
Put them back in after you have run the processor.

I have only come across the C preprocessor as a seperate program called
'cpp' on *nix boxes. It may be available on windows but I do not know where
or how.
Colin.

"Timex" <su********@hot mail.com> wrote in message
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.

Nov 13 '05 #7
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.


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

--
Tim Hagan
Nov 13 '05 #8
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.

Jeremy.
Nov 13 '05 #9
> 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?
Nov 13 '05 #10

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

Similar topics

2
8806
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
1495
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
8947
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
2930
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
9425
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
10225
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
10001
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
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
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...
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.