Эта принципиальная тривиальная модификация вашего кода касается нескольких проблем в вашем коде.
- Вы не должны использовать
feof()
вот так - `while (!feof(file)) is always wrong.
- Вы не должны читать данные, которые не являются частью прочитанной строки.
Я также отредактировал ваш код так, что функция принимает имя файла, открывает, подсчитывает и закрывает его, а также сообщает о найденном.
#include <stdio.h>
#include <string.h>
// Revised interface - process a given file name, reporting
static void commentChars(char const *file)
{
char str[256];
FILE *fp;
long commentCount = 0;
if (!(fp = fopen(file, "r")))
{
fprintf(stderr, "Error! File %s not found\n", file);
return;
}
while (fgets(str, sizeof(str), fp) != 0)
{
int len = strlen(str);
for (int i = 0; i <= len; i++)
{
if (str[i] == '/' && str[i + 1] == '/')
{
commentCount += (strlen(str) - 2);
break;
}
}
}
fclose(fp);
printf("%s: Number of characters contained in comments: %ld\n", file, commentCount);
}
int main(int argc, char **argv)
{
if (argc == 1)
commentChars("/dev/stdin");
else
{
for (int i = 1; i < argc; i++)
commentChars(argv[i]);
}
return 0;
}
При запуске на исходном коде (ccc.c
), он дает:
ccc.c: Number of characters contained in comments: 58
Комментарий не очень полный (ой), но он служит, чтобы показать, что происходит. Он подсчитывает новую строку, которую fgets()
сохраняет как часть комментария, хотя инсталлятор //
не учитывается.
Работа с /*
комментариев сложнее. Вам нужно заметить косую черту, за которой следует звезда, а затем прочитать до следующей пары символов слэш-символа. Это, вероятно, легче сделать с использованием символа с помощью ввода символов, чем по очереди; вы, по крайней мере, должны иметь возможность чередовать анализ символов с помощью линейного ввода.
Когда вы будете готовы к этому, вы можете попробовать этот тест на пытку в своей программе. Это то, что я использую для проверки моего стриппера комментария, SCC (который не обрабатывает триграфы - сознательным решением, если источник содержит триграфы, у меня есть средство для удаления триграфа, которое я использую сначала в источнике).
/*
@(#)File: $RCSfile: scc.test,v $
@(#)Version: $Revision: 1.7 $
@(#)Last changed: $Date: 2013/09/09 14:06:33 $
@(#)Purpose: Test file for program SCC
@(#)Author: J Leffler
*/
/*TABSTOP=4*/
// -- C++ comment
/*
Multiline C-style comment
#ifndef lint
static const char sccs[] = "@(#)$Id: scc.test,v 1.7 2013/09/09 14:06:33 jleffler Exp $";
#endif
*/
/*
Multi-line C-style comment
with embedded /* in line %C% which should generate a warning
if scc is run with the -w option
Two comment starts /* embedded /* in line %C% should generate one warning
*/
/* Comment */ Non-comment /* Comment Again */ Non-Comment Again /*
Comment again on the next line */
// A C++ comment with a C-style comment marker /* in the middle
This is plain text under C++ (C99) commenting - but comment body otherwise
// A C++ comment with a C-style comment end marker */ in the middle
The following C-style comment end marker should generate a warning
if scc is run with the -w option
*/
Two of these */ generate */ one warning
It is possible to have both warnings on a single line.
Eg:
*/ /* /* */ */
SCC has been trained to handle 'q' single quotes in most of
the aberrant forms that can be used. '\0', '\\', '\'', '\\
n' (a valid variant on '\n'), because the backslash followed
by newline is elided by the token scanning code in CPP before
any other processing occurs.
This is a legitimate equivalent to '\n' too: '\
\n', again because the backslash/newline processing occurs early.
The non-portable 'ab', '/*', '*/', '//' forms are handled OK too.
The following quote should generate a warning from SCC; a
compiler would not accept it. '
\n'
" */ /* SCC has been trained to know about strings /* */ */"!
"\"Double quotes embedded in strings, \\\" too\'!"
"And \
newlines in them"
"And escaped double quotes at the end of a string\""
aa '\\
n' OK
aa "\""
aa "\
\n"
This is followed by C++/C99 comment number 1.
// C++/C99 comment with \
continuation character \
on three source lines (this should not be seen with the -C flag)
The C++/C99 comment number 1 has finished.
This is followed by C++/C99 comment number 2.
/\
/\
C++/C99 comment (this should not be seen with the -C flag)
The C++/C99 comment number 2 has finished.
This is followed by regular C comment number 1.
/\
*\
Regular
comment
*\
/
The regular C comment number 1 has finished.
/\
\/ This is not a C++/C99 comment!
This is followed by C++/C99 comment number 3.
/\
\
\
/But this is a C++/C99 comment!
The C++/C99 comment number 3 has finished.
/\
\* This is not a C or C++ comment!
This is followed by regular C comment number 2.
/\
*/ This is a regular C comment *\
but this is just a routine continuation *\
and that was not the end either - but this is *\
\
/
The regular C comment number 2 has finished.
This is followed by regular C comment number 3.
/\
\
\
\
* C comment */
The regular C comment number 3 has finished.
Note that \u1234 and \U0010FFF0 are legitimate Unicode characters
(officially universal character names) that could appear in an
id\u0065ntifier, a '\u0065' character constant, or in a "char\u0061cter\
string". Since these are mapped long after comments are eliminated,
they cannot affect the interpretation of /* comments */. In particular,
none of \u0002A. \U0000002A, \u002F and \U0000002F ever constitute part
of a comment delimiter ('*' or '/').
More double quoted string stuff:
if (logtable_out)
{
sprintf(logtable_out,
"insert into %s (bld_id, err_operation, err_expected, err_sql_stmt, err_sql_state)"
" values (\"%s\", \"%s\", \"%s\", \"", str_logtable, blade, operation, expected);
/* watch out for embedded double quotes. */
}
/* Non-terminated C-style comment at the end of the file
Может быть работа для [гибкого] (Http: //flex.sourceforge.net /) ... –
Вы используете очень опасный код. Вы должны держаться подальше от 'fflush (stdin)' и 'gets()'. – edmz
Обратите внимание, что выполнять работу очень сложно. Основы не так уж плохи, но вам нужно знать о символах обратной косой черты, указывающих на сращивание строк, и о символьных константах (''/* ''- это не начало комментария, это многосимвольная константа) и строки (''/* это не комментарий */"' - [ceci n'est pas une pipe] (https://en.wikipedia.org/wiki/The_Treachery_of_Images) и Magritte?). Триграфы также являются (теоретической) проблемой. –