gpt4 book ai didi

c - 在 C 中删除内联注释时的意外行为

转载 作者:太空宇宙 更新时间:2023-11-04 00:57:25 26 4
gpt4 key购买 nike

堆栈溢出!我正在学习C技术。我有一个获取输入文件、查找文件并将内容写入输出文件而不带注释的函数。该功能有效,但在某些情况下也会刹车。我的功能:

    void removeComments(char* input, char* output)
{
FILE* in = fopen(input,"r");
FILE* out = fopen(ouput,"w");
char c;
while((c = fgetc(in)) != EOF)
{
if(c == '/')
{

c = fgetc(in);
if(c == '/')
{
while((c = fgetc(in)) != '\n');
}
else
{
fputc('/', out);
}
}
else
{
fputc(c,out);
}
}
fclose(in);
fclose(out);
}

但是当我将这样的文件作为输入时:

// Parameters: a, the first integer; b the second integer.
// Returns: the sum.
int add(int a, int b)
{
return a + b; // An inline comment.
}
int sample = sample;

当删除内联注释时,由于某种原因它无法到达 '\n' 并给出输出:

int add(int a, int b) 
{
return a + b; }
int sample = sample;

[编辑]谢谢你帮助我!它适用于我发布的案例,但它在另一个案例中刹车。当前代码:

FILE* in = fopen(input,"r");
FILE* out = fopen(output,"w");

if (in == NULL) {
printf("cannot read %s\n", input);
return; /* change signature to return 0 ? */
}
if (out == NULL) {
printf("cannot write in %s\n", output);
return; /* change signature to return 0 ? */
}

int c;
int startline = 1;

while((c = fgetc(in)) != EOF)
{
if(c == '/')
{
c = fgetc(in);

if(c == '/')
{
while((c = fgetc(in)) != '\n')
{
if (c == EOF) {
fclose(in);
fclose(out);
return; /* change signature to return 1 ? */
}
}
if (! startline)
fputc('\n', out);
startline = 1;
}
else if (c == EOF)
break;
else {
fputc('/', out);
startline = 0;
}
}
else
{
fputc(c,out);
startline = (c == '\n');
}
}

fclose(in);
fclose(out);

当文件包含除法时,第二个变量消失。示例:

int divide(int a, int b) 
{
return a/b;
}

它回馈:

int divide(int a, int b) 
{
return a/;
}

最佳答案

之后

while((c = fgetc(in)) != '\n');

你需要一个fputc('\n', out);

补充说明:

char c;
while((c = fgetc(in)) != EOF)

c 必须是 int 才能管理 EOF

只是一个错字:ouput 必须是output 才能编译

在你读到一个 '/' 之后,你没有很好地管理 EOF

您错过了检查fopen的结果


一个建议:

#include <stdio.h>

void removeComments(char* input, char* output)
{
FILE* in = fopen(input,"r");
FILE* out = fopen(output,"w");

if (in == NULL) {
printf("cannot read %s\n", input);
return; /* change signature to return 0 ? */
}
if (out == NULL) {
printf("cannot write in %s\n", output);
return; /* change signature to return 0 ? */
}

int c;

while((c = fgetc(in)) != EOF)
{
if(c == '/')
{
c = fgetc(in);

if(c == '/')
{
while((c = fgetc(in)) != '\n')
{
if (c == EOF) {
fclose(in);
fclose(out);
return; /* change signature to return 1 ? */
}
}
fputc('\n', out);
}
else if (c == EOF) {
fputc('/', out);
break;
}
else
fputc('/', out);
fputc(c, out);
}
else
{
fputc(c,out);
}
}

fclose(in);
fclose(out);
/* change signature to return 1 ? */
}

int main(int argc, char ** argv)
{
removeComments(argv[1], argv[2]);
}

正如 Tormund Giantsbane 在评论中所说,最好完全删除仅包含评论的行(评论从第一列开始),新提案就是这样做的:

#include <stdio.h>

void removeComments(char* input, char* output)
{
FILE* in = fopen(input,"r");
FILE* out = fopen(output,"w");

if (in == NULL) {
printf("cannot read %s\n", input);
return; /* change signature to return 0 ? */
}
if (out == NULL) {
printf("cannot write in %s\n", output);
return; /* change signature to return 0 ? */
}

int c;
int startline = 1;

while((c = fgetc(in)) != EOF)
{
if(c == '/')
{
c = fgetc(in);

if(c == '/')
{
while((c = fgetc(in)) != '\n')
{
if (c == EOF) {
fclose(in);
fclose(out);
return; /* change signature to return 1 ? */
}
}
if (! startline)
fputc('\n', out);
startline = 1;
}
else if (c == EOF) {
fputc('/', out);
break;
}
else {
fputc('/', out);
fputc(c, out);
startline = 0;
}
}
else
{
fputc(c,out);
startline = (c == '\n');
}
}

fclose(in);
fclose(out);
/* change signature to return 1 ? */
}

int main(int argc, char ** argv)
{
removeComments(argv[1], argv[2]);
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -g r.c
pi@raspberrypi:/tmp $ cat i
// Parameters: a, the first integer; b the second integer.
// Returns: the sum.
int add(int a, int b)
{
return a + b/c; // An inline comment.
}
int sample = sample;
pi@raspberrypi:/tmp $ ./a.out i o
pi@raspberrypi:/tmp $ cat o
int add(int a, int b)
{
return a + b/c;
}
int sample = sample;

正如 DavidC 所说。在注释中,如果//放在字符串中,结果将不是预期的结果,字符甚至非法也是这种情况(我的意思是 '//' 不能更改),C 注释(/* ..//... */) 等

关于c - 在 C 中删除内联注释时的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54912093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com