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


Please Help Members By Posting Answers For Below Questions

Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.

1491


Explain how can I right-justify a string?

618


The performance of an operation in several steps with each step using the output of the preceding step a) recursion b) search c) call by value d) call by reference

669


What is typedef example?

614


How many levels of pointers can you have?

695






Why enum is used in c?

523


When we use void main and int main?

588


a single linked list consists of nodes a to z .print the nodes in reverse order from z to a using recursion

2330


Explain what could possibly be the problem if a valid function name such as tolower() is being reported by the c compiler as undefined?

575


What is the difference between c and python?

579


What is variable initialization and why is it important?

613


How can you determine the maximum value that a numeric variable can hold?

633


‘ C’ PROGRAME TO SHOW THE TYPE OF TRANGLE BY ACCEPTING IT’S LENGTH .

2368


How can I determine whether a machines byte order is big-endian or little-endian?

615


How many levels of pointers have?

589