gpt4 book ai didi

c - 如何在给定程序中打印评论?

转载 作者:行者123 更新时间:2023-12-02 05:43:26 26 4
gpt4 key购买 nike

我想将评论打印到给定程序以在控制台中打印?

在程序中是否有任何函数和自己的逻辑来打印评论?

例如

 int main()
{
/* this is a comment */
}

上面的程序只有一条评论我想通过某种逻辑或任何函数将这条评论打印到控制台,如果在 C 中有的话?

最佳答案

您需要编写一个程序来读取 c 源文件,并识别和打印这些文件中的注释。

#include <stdio.h>

void read_until_char (FILE *f, char c) {
// read file until c is found
}

void read_until_char_into (FILE *f, char c, char *buf) {
// read file until c is found
// also write each char read into buffer
}

void print_comments(const char *filename) {
FILE *f = fopen(filename, "r");
char buffer[2048];
int c;
if (f) {
while (!feof(f)) {
read_until_char(f, '/');
c = fgetc(f);
if (c == '*') {
while (!feof(f)) {
read_until_char_into(f, '*', buffer);
if ((c=fgetc(f)) == '/') {
// print buffer
break;
} else if (c == '*') {
// consider what to do if c is *
}
}
} else if (c == '/') {
read_until_char_into(f, '\n', buffer);
// print buffer
}
}
fclose(f);
}
}

关于c - 如何在给定程序中打印评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12159055/

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