Thread: Deleting comments in another program

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Deleting comments in another program

    This program needs to delete the comments in another c program. So what I tried to do was just copy it to a new file.
    Code:
    #include <stdio.h>
    void filecopy(FILE *, FILE *);
    main()
    {
       FILE *fin;
       FILE *fout;
       fin=fopen("attempt4.c","r");
       if((fout=fopen("pw.c","w"))==NULL)
        printf("GERRRRR");
       else
       filecopy(fin,fout);
       
       fclose(fin);
    
    return 0;
    }
    void filecopy(FILE *ifp, FILE *ofp)
    {
       int c;
       int prevc;
       while((c=getc(ifp))!=EOF)
       {
        if(prevc='/') //look for first char of a comment
           if(c='/') //find second one
           {
             while(c !='\n')
             {
            //do nothing
             }
               }
            
           else if(c='*') // find /* incase it is blocked
           {
            /*
            doesn't stop until it finds
            end of comment
            */
              while(prevc !='*' && c!='/') 
              {
            //do nothing
              }
           }
        else //copy the rest to the other file
        {
           putc(c,ofp);
           prevc=getc(ifp);
        }
       }
    }
    my problem is that I'm not sure how exactly to call it, I don't know if i'm doing that part right. When I run the program it doesn't do anything and I can't exit the terminal. Please help!!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It looks like you're on the right track. Some problems I see:
    • You need to give prevc an initial value before the loop that isn't a slash. '\0' would be pretty safe. Otherwise it can contain anything, and while a '/' is unlikely, it's possible, and may match a comment that isn't there.
    • Don't assign prevc the value from getc(ifp). That makes it the next character. You want to assign it the value of c. Also, don't do it in the else block, do it as the last statement in the loop, regardless of whether you're in a comment.
    • You need to check both file pointers for NULL after you open them. If either one is NULL, you can't open the file, so you need to close any open ones, print an error and quit.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Remember that there is a difference between assignment = and equality testing ==




    Congratulations on using an int with getc instead of a char... kudos to you.




    What happens if you run across this bit of code:
    Code:
    printf("// This is not a comment"); // This is a comment
    You must know if the // you have read is a part of string literal enclosed in quotes or not otherwise you'd chop off the above and output it as:
    Code:
    printf("



    Don't know how to call what? The filecopy function? Looks like the call itself is fine though the prototype should not have the argument names removed as a simple matter of good coding practice.




    I have not looked at the logic you are using but the above is what I noticed so far.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This is an infinite loop because you're not reading in any new characters:
    Code:
             while(c !='\n')
             {
            //do nothing
             }
    The other similar loop is also an infinite loop.

    That's why your program sits there doing nothing.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    There are also issues with the use of the assignment equals (=) used where the comparison equals (==) should be used .

    if(prevc='/') //look for first char of a comment
    if(c='/') //find second one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to remove comments. Not working
    By alter.ego in forum C Programming
    Replies: 29
    Last Post: 10-01-2011, 01:58 AM
  2. program to remove comments from source
    By Abda92 in forum C Programming
    Replies: 12
    Last Post: 12-25-2006, 05:18 PM
  3. comments my program
    By ssharish in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2005, 09:53 AM
  4. I need comments and answers for this C program
    By BadProgrammer in forum C Programming
    Replies: 2
    Last Post: 05-25-2003, 10:57 PM
  5. Replies: 5
    Last Post: 12-05-2001, 07:37 AM