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)