Thread: How to detect and remove the comments in a C source file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    How to detect and remove the comments in a C source file

    I have been looking at a problem of detecting and removing the comment lines. But the program just stops. Here is the code:


    Code:
    #include <stdio.h>
    // This code removes the comments from a file while moving the non-comments to another file.
    FILE *fs, *ft;
    int main ()
    {
    char source[67], target[67];
    int c,d;
    puts ("Enter C file name");
    gets (source);
    fs = fopen(source, "r");
    if ( fs==NULL)
      {
       puts ("Can not open source file");
      }
    puts ("Enter target name");
    gets (target);
    ft = fopen (target, "w");
    if (ft == NULL)
    {
      puts ("Can not open the target file");
    }
    while ( (c = getc(fs)) != EOF)
      {
        if ( c == '/')
          {
            if ( (d = getc(fs)) == '*')
            incomment();
            else
              {
                 fprintf ( ft, "%c", c);
                 fprintf ( ft, "%c", d);
              }
          }
        else
        fprintf(ft, "%c", c);
      }
      fclose (fs);
      fclose (ft);

    I tried to use GDB, and I want to ask that besides moving the breakpoint further down the code, and press Continue, Print, and Step, is there any other feature of GDB that I can use to quickly find out where the program stops?

    I found Remove comments from C/C++ code - Stack Overflow and Jonathan Leffler's post implies that there are different ways of commenting in C. I think it is really clumsy to have to keep adding if blocks for every possible way to comment in C source files. Is there a more elegant way of doing this? Note, this is just an exercise.

    When I run GDB I get the following, and I do not know why I get such a large number of c. I also can not figure out why I can not backtrack.
    Code:
    Enter C file name
    test.c
    Enter target name
    output
    Breakpoint 1, incomment () at gd.c:44
    44      c = getc(fs);
    (gdb) p c
    $1 = 1982321136
    (gdb) bt
    #0  incomment () at gd.c:44
    #1  0x004014dc in main () at gd.c:27
    (gdb) bt
    #0  incomment () at gd.c:44
    #1  0x004014dc in main () at gd.c:27
    (gdb) bt
    #0  incomment () at gd.c:44
    #1  0x004014dc in main () at gd.c:27
    (gdb)
    more detailed debug output

    Code:
    Enter C file name
    test.c
    Enter target name
    output
    Breakpoint 1, main () at gd.c:27
    27              incomment();
    (gdb) p c
    $1 = 47
    (gdb) p d
    $2 = 42
    (gdb) s
    incomment () at gd.c:44
    44      c = getc(fs);
    (gdb) p c
    $3 = 1982321136
    (gdb)
    Last edited by zolfaghar; 06-04-2016 at 03:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Test at http://www.artlogic.com/careers/test.html
    By zMan in forum C++ Programming
    Replies: 6
    Last Post: 07-15-2003, 06:11 AM
  2. IQ Test
    By DarkViper in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 11-24-2002, 01:42 AM

Tags for this Thread