Skip to main content
added 10 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I wrote the belowthis program to remove comments from a C source file, accepting a file name from the command prompt. Let me know if I missed anything or if there are any improvements that can be made in the program.

I wrote the below program to remove comments from a C source file, accepting file name from the command prompt. Let me know if I missed anything or any improvements that can be made in the program.

I wrote this program to remove comments from a C source file, accepting a file name from the command prompt. Let me know if I missed anything or if there are any improvements that can be made in the program.

deleted 83 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Remove Removing comments from a C source file

I wrote the below program to remove comments from a cC source file. Accepting, accepting file name from the command Promptprompt. SuggestLet me Ifknow if I missed anything or any Improvementsimprovements that can be made in the program.

    #include<stdio.h>
    #include<stdbool.h>

    void m_cmnt( FILE *fp )
    {
        int prev=0 , ch ;

        while ((ch=getc(fp))!=EOF)
        {
            if ( prev== '*' && ch == '/')
                return ;
            else
            prev=ch ;
        }
    }

    void s_cmnt( FILE *fp )
    {
        int ch ;
        while ( (ch=getc(fp))!='\n' && ch!=EOF )
            ;
    }

    int main ( int c , char **arr )
    {
            FILE *fp , *np ;
            int ch , prev=0 ;
            bool String=0 ;

            fp = fopen(arr[1] , "r") ;
            np = fopen("temp.txt" , "w") ;

            if (fp==NULL )
            {
                printf ("Invalid/No Filename given as Argument ! \n") ;
                return 1 ;
            }
            while ((ch=getc(fp))!=EOF )
            {
                /* file pointer currently not inside a string */
                if ( !String )
                {
                    if ( ch=='/' )
                    {
                        prev=ch ;
                        ch= getc(fp) ;
                        switch(ch)
                        {
                            case '*'  : m_cmnt( fp) ;
                                         break ;
                            case '/'  : s_cmnt( fp) ;
                                         break ;
                            default   :  putc(prev , np) ;
                                         putc(ch , np) ;
                                         break ;
                        }
                    }
                    else putc( ch ,np ) ;
                }
                else  putc(ch , np) ;

                if ( ch=='\"')
                    String = !String ;
                prev = ch ;
            }

        fclose(fp) ;
        fclose(np) ;
        remove(arr[1]) ;
        rename( "temp.txt" , arr[1] ) ;
        return 0 ;
    }

Thank you...

Remove comments from C source file

I wrote the below program to remove comments from a c source file. Accepting file name from command Prompt. Suggest me If I missed anything or any Improvements that can be made in the program.

    #include<stdio.h>
    #include<stdbool.h>

    void m_cmnt( FILE *fp )
    {
        int prev=0 , ch ;

        while ((ch=getc(fp))!=EOF)
        {
            if ( prev== '*' && ch == '/')
                return ;
            else
            prev=ch ;
        }
    }

    void s_cmnt( FILE *fp )
    {
        int ch ;
        while ( (ch=getc(fp))!='\n' && ch!=EOF )
            ;
    }

    int main ( int c , char **arr )
    {
            FILE *fp , *np ;
            int ch , prev=0 ;
            bool String=0 ;

            fp = fopen(arr[1] , "r") ;
            np = fopen("temp.txt" , "w") ;

            if (fp==NULL )
            {
                printf ("Invalid/No Filename given as Argument ! \n") ;
                return 1 ;
            }
            while ((ch=getc(fp))!=EOF )
            {
                /* file pointer currently not inside a string */
                if ( !String )
                {
                    if ( ch=='/' )
                    {
                        prev=ch ;
                        ch= getc(fp) ;
                        switch(ch)
                        {
                            case '*'  : m_cmnt( fp) ;
                                         break ;
                            case '/'  : s_cmnt( fp) ;
                                         break ;
                            default   :  putc(prev , np) ;
                                         putc(ch , np) ;
                                         break ;
                        }
                    }
                    else putc( ch ,np ) ;
                }
                else  putc(ch , np) ;

                if ( ch=='\"')
                    String = !String ;
                prev = ch ;
            }

        fclose(fp) ;
        fclose(np) ;
        remove(arr[1]) ;
        rename( "temp.txt" , arr[1] ) ;
        return 0 ;
    }

Thank you...

Removing comments from a C source file

I wrote the below program to remove comments from a C source file, accepting file name from the command prompt. Let me know if I missed anything or any improvements that can be made in the program.

#include<stdio.h>
#include<stdbool.h>

void m_cmnt( FILE *fp )
{
    int prev=0 , ch ;

    while ((ch=getc(fp))!=EOF)
    {
        if ( prev== '*' && ch == '/')
            return ;
        else
        prev=ch ;
    }
}

void s_cmnt( FILE *fp )
{
    int ch ;
    while ( (ch=getc(fp))!='\n' && ch!=EOF )
        ;
}

int main ( int c , char **arr )
{
        FILE *fp , *np ;
        int ch , prev=0 ;
        bool String=0 ;

        fp = fopen(arr[1] , "r") ;
        np = fopen("temp.txt" , "w") ;

        if (fp==NULL )
        {
            printf ("Invalid/No Filename given as Argument ! \n") ;
            return 1 ;
        }
        while ((ch=getc(fp))!=EOF )
        {
            /* file pointer currently not inside a string */
            if ( !String )
            {
                if ( ch=='/' )
                {
                    prev=ch ;
                    ch= getc(fp) ;
                    switch(ch)
                    {
                        case '*'  : m_cmnt( fp) ;
                                     break ;
                        case '/'  : s_cmnt( fp) ;
                                     break ;
                        default   :  putc(prev , np) ;
                                     putc(ch , np) ;
                                     break ;
                    }
                }
                else putc( ch ,np ) ;
            }
            else  putc(ch , np) ;

            if ( ch=='\"')
                String = !String ;
            prev = ch ;
        }

    fclose(fp) ;
    fclose(np) ;
    remove(arr[1]) ;
    rename( "temp.txt" , arr[1] ) ;
    return 0 ;
}
added 20 characters in body; added 3 characters in body
Source Link
user93907
user93907
    #include<stdio.h>
    #include<stdbool.h>

    void m_cmnt( FILE *fp )
    {
        int prev=0 , ch ;

        while ((ch=getc(fp))!=EOF)
        {
            if ( prev== '*' && ch == '/')
                return ;
            else
            prev=ch ;
        }
    }

    void s_cmnt( FILE *fp )
    {
        int ch ;
        while ( (ch=getc(fp))!='\n' && ch!=EOF )
            ;
    }

    int main ( int c , char **arr )
    {
            FILE *fp , *np ;
            int ch , prev=0 ;
            bool String=0 ;

            fp = fopen(arr[1] , "r") ;
            np = fopen("temp.txt" , "w") ;

            if (fp==NULL )
            {
                printf ("Invalid/No Filename given as Argument ! \n") ;
                return 1 ;
            }
            while ((ch=getc(fp))!=EOF )
            {
                /* file pointer currently not inside a string */
                if ( !String )
                {
                    if ( ch=='/' )
                    {
                        prev=ch ;
                        ch= getc(fp) ;
                        switch(ch)
                        {
                            case '*'  : m_cmnt( fp) ;
                                         break ;
                            case '/'  : s_cmnt( fp) ;
                                         break ;
                            default   :  putc(prev , np) ;
                                         putc(ch , np) ;
                                         break ;
                        }
                    }
                    else putc( ch ,np ) ;
                }
                else  putc(ch , np) ;

                if ( ch=='\"')
                    String = !String ;
                prev = ch ;
            }

        fclose(fp) ;
        fclose(np) ;
        remove(arr[1]) ;
        rename( "temp.txt" , arr[1] ) ;
        return 0 ;
    }
    #include<stdio.h>
    #include<stdbool.h>

    void m_cmnt( FILE *fp )
    {
        int prev=0 , ch ;

        while ((ch=getc(fp))!=EOF)
        {
            if ( prev== '*' && ch == '/')
                return ;
            else
            prev=ch ;
        }
    }

    void s_cmnt( FILE *fp )
    {
        int ch ;
        while ( (ch=getc(fp))!='\n' && ch!=EOF )
            ;
    }

    int main ( int c , char **arr )
    {
            FILE *fp , *np ;
            int ch , prev=0 ;
            bool String=0 ;

            fp = fopen(arr[1] , "r") ;
            np = fopen("temp.txt" , "w") ;

            if (fp==NULL )
            {
                printf ("Invalid/No Filename given as Argument ! \n") ;
                return 1 ;
            }
            while ((ch=getc(fp))!=EOF )
            {
                /* file pointer currently not inside a string */
                if ( !String )
                {
                    if ( ch=='/' )
                    {
                        prev=ch ;
                        ch= getc(fp) ;
                        switch(ch)
                        {
                            case '*'  : m_cmnt( fp) ;
                                         break ;
                            case '/'  : s_cmnt( fp) ;
                                         break ;
                            default   :  putc(prev , np) ;
                                         putc(ch , np) ;
                                         break ;
                        }
                    }
                    else putc( ch ,np ) ;
                }
                else  putc(ch , np) ;

                if ( ch=='\"')
                    String = !String ;
                prev = ch ;
            }

        fclose(fp) ;
        fclose(np) ;
        remove(arr[1]) ;
        rename( "temp.txt" , arr[1] ) ;
    }
    #include<stdio.h>
    #include<stdbool.h>

    void m_cmnt( FILE *fp )
    {
        int prev=0 , ch ;

        while ((ch=getc(fp))!=EOF)
        {
            if ( prev== '*' && ch == '/')
                return ;
            else
            prev=ch ;
        }
    }

    void s_cmnt( FILE *fp )
    {
        int ch ;
        while ( (ch=getc(fp))!='\n' && ch!=EOF )
            ;
    }

    int main ( int c , char **arr )
    {
            FILE *fp , *np ;
            int ch , prev=0 ;
            bool String=0 ;

            fp = fopen(arr[1] , "r") ;
            np = fopen("temp.txt" , "w") ;

            if (fp==NULL )
            {
                printf ("Invalid/No Filename given as Argument ! \n") ;
                return 1 ;
            }
            while ((ch=getc(fp))!=EOF )
            {
                /* file pointer currently not inside a string */
                if ( !String )
                {
                    if ( ch=='/' )
                    {
                        prev=ch ;
                        ch= getc(fp) ;
                        switch(ch)
                        {
                            case '*'  : m_cmnt( fp) ;
                                         break ;
                            case '/'  : s_cmnt( fp) ;
                                         break ;
                            default   :  putc(prev , np) ;
                                         putc(ch , np) ;
                                         break ;
                        }
                    }
                    else putc( ch ,np ) ;
                }
                else  putc(ch , np) ;

                if ( ch=='\"')
                    String = !String ;
                prev = ch ;
            }

        fclose(fp) ;
        fclose(np) ;
        remove(arr[1]) ;
        rename( "temp.txt" , arr[1] ) ;
        return 0 ;
    }
Source Link
user93907
user93907
Loading