How to read a text or a .c file without comments into a cell array ?
Mostrar comentarios más antiguos
I have a c file name 'function.c'
It have code and comments as well comments are noted with '/*' '*/' Or '//' OR sometimes '%'
I just have to read the content of code not the comments
I have tried : 1) fid=fopen('function.c') code = textscan(fid,'%s','CommentStyle',{'/*','*/'})
but this help me in excluding comments in between '/*' and '*/'
2) fid=fopen('function.c') code = textscan(fid,'%s','CommentStyle','//')
this excludes only comments with '//'
Are there any ways to excludes both types of comments ??
1 comentario
Don't think so in on go...best I can think of is to read it with the first option and write that as a temporary file then process that file with the second.
I think textscan will only handle the case of '/*' '*/' pairs if they're first on the line, however, so perhaps you could get the same effect by reading the whole file as a char() array w/ fread, do a global substitution for strrep('/*','//') and then process that from memory?
Failing that, since one would presume c source files aren't terribly long, read line-by-line w/ fgetl and do the search on each line for either. This will let you get the end of line comments to as a side benefit to make up for the extra complexity...
ADDENDUM:
I reviewed the doc's for textscan; looks like it would catch the /* */ sequences correctly; what I recalled as beginning of line is actually written/documented as beginning of field.
Respuesta aceptada
Más respuestas (2)
What about a simple..
buffer = fileread( 'function.c' ) ;
buffer = regexprep( buffer, '/\*.*?\*/', '' ) ;
buffer = regexprep( buffer, '//.*?(\r)?\n', '$1\n' ) ;
which seems to be working in standard situations. Of course, managing all situations including pathological ones would require more work, but this has the advantage to be simple and you might not need to manage pathological cases, especially if you wrote these C files yourself, consistently and with rigor.
PS: I tested it with the following content
NotComment 1 /* Comment */
// Comment
/* Comment */ NotComment 2
NotComment 3 // Comment
/* Comment // Comment */ NotComment 4
/* Comment
Comment // Comment
Comment */
NotComment 5
2 comentarios
Jan
el 20 de Sept. de 2013
@Cedric: Providing your test data is a rock solid definition of the capabilities of your code. While regexp expressions always look like somebody has rolled an angry armadillo over the keyboard, the test data are clear due to their simplicity. +1
Cedric
el 20 de Sept. de 2013
Thank you Jan. Well, providing the test data shows that I am not managing this situation that you listed
this is contents // Here a C comment starts /*
this is contents // Here the C comment ends */
We could actually, but I'll see what the OP answers before making patterns more complex.
Simon
el 19 de Sept. de 2013
Hi!
I usually do it this way:
* read full file with textscan
* define cell array of comment strings
* use regexp to find all lines with a comment string in it (or in front of)
* delete the lines found
2 comentarios
Jan
el 19 de Sept. de 2013
- ignore comment keys in quotes strings
- consider multi-line comments
Simon
el 19 de Sept. de 2013
Yes, my list was not complete. I'm sure it can be enlarged with many more entries! It's easier if you have a fixed-format source code like I usually have with Fortran 77. You just search for "C" in column 1 ...
Categorías
Más información sobre Standard File Formats en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!