Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to take the name of a file with the suffix \".c\", create a file for outp

ID: 3544096 • Letter: I

Question

I need to take the name of a file with the suffix ".c", create a file for output with the same ame replacing ".c" with ".i".

The program needs to remove all comments (/* and */) and comments that begin with "//". Comments taht do not end are errors and should be decteded and message should be generated. It must recognize and process the #define statement. Recognizing statements like "#define SYMBOL value" and when it recognizes it should remember the SYMBOL and its value and not generate any output for that line - redefining a SYMBOL that already has been defined is an error and should generate a message. It should recognize a form of #ifdef and #endif; if the program finds "#ifdef SYMBOL and if SYMBOL is not defined, it should replace aeach line of input with a blank line until it reads an #endif - missing #endif should generate an error message; if SYMBOL has been defined, and if that SYMBOL is found as a token, then SYMBOL should be replaced with the defined value in the output - symbols inside of quoted strings should not be replaced. any preprocessor directived formatted in any other way should be copiet to the output unchanged.

Explanation / Answer

#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 ( " The File
Name :%s, Read Problem", file_name );
free ( cp_file_content );
cp_file_content = NULL;
} else {
cp_file_content [
_n_bytes ] = '';
}
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 ( " 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 ) != '' ) {
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_counter++;
}
printf ( "%c", * ( cp_file_content
+ n_counter ) );
}
}
}