Write a program to remove the C comments(/* */) and C++
comments(//) from a file.
The file should be declared in command line.
Answer Posted / abdur rab
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int get_file_size ( const char* file_name )
{
struct stat _file_info;
int _n_bytes = 0;
if ( 0 <= stat ( file_name, &_file_info ) ) {
if ( S_ISREG ( _file_info.st_mode ) )
_n_bytes = _file_info.st_size;
}
return ( _n_bytes );
}
char* read_content ( const char* file_name )
{
FILE* _file_pointer = NULL;
char* cp_file_content = NULL;
int _n_bytes = 0;
_n_bytes = get_file_size ( file_name );
cp_file_content = (char*) malloc ( ( _n_bytes + 1 )
* sizeof ( char ) );
if ( NULL != cp_file_content ) {
_file_pointer = fopen ( file_name, "r" );
if ( _file_pointer ) {
if ( _n_bytes != fread (
cp_file_content, 1, _n_bytes, _file_pointer ) ) {
printf ( "\n The File
Name :%s, Read Problem", file_name );
free ( cp_file_content );
cp_file_content = NULL;
} else {
cp_file_content [
_n_bytes ] = '\0';
}
fclose ( _file_pointer );
}
}
return ( cp_file_content );
}
int main ( int argc, char* argv [] )
{
char* cp_file_content = NULL;
int n_length = 0;
int n_counter = 0;
if ( argc <= 1 || argc > 3 ) {
printf ( "\n Usage : comment_remover
<file_name>" );
exit ( 0 );
}
cp_file_content = read_content ( argv [ 1 ] );
if ( NULL != cp_file_content ) {
n_length = strlen ( cp_file_content );
for ( n_counter = 0; n_counter < n_length;
n_counter++ ) {
if ( ( * ( cp_file_content +
n_counter ) == '/' )
&& ( * (
cp_file_content + n_counter + 1 ) == '*' ) ) {
while ( * ( cp_file_content
+ n_counter ) != '\0' ) {
n_counter++;
if ( ( * (
cp_file_content + ( n_counter - 1 ) ) == '*' )
&&
( * ( cp_file_content + n_counter ) == '/' ) ) {
n_counter++; // move away from / (slash)
break;
}
}
} else if ( ( * ( cp_file_content +
n_counter ) == '/' )
&& ( * (
cp_file_content + n_counter + 1 ) == '/' ) ) {
while ( * ( cp_file_content
+ n_counter ) != '\n' ) n_counter++;
}
printf ( "%c", * ( cp_file_content
+ n_counter ) );
}
}
}
| Is This Answer Correct ? | 10 Yes | 23 No |
Post New Answer View All Answers
What is the use of #define preprocessor in c?
Write a program to show the change in position of a cursor using c
Why is it usually a bad idea to use gets()? Suggest a workaround.
What are the difference between a free-standing and a hosted environment?
What is the use of function in c?
What is assert and when would I use it?
write a c program to do the following: a) To find the area of a triangle. b) To convert the temperature from Fahrenheit to Celsius. c) To convert the time in hours : minutes : seconds to seconds.
A text file that contains declarations used by a group of functions,programs,or users a) executable file b) header file c) obj file d) .cfile
What is a pointer variable in c language?
If fflush wont work, what can I use to flush input?
What does == mean in texting?
what is the difference between 123 and 0123 in c?
void main(){ int a; a=1; while(a-->=1) while(a-->=0); printf("%d",a); }
What is enumerated data type in c?
What should malloc(0) do? Return a null pointer or a pointer to 0 bytes?