-1

我想在 C 中将以下 html 打印到控制台。但是,我的代码中有一个错误让我非常困惑,我不知道这怎么会发生?错误是它再次打印最后一个字符数组。

样本输出:

<html>
    <body>
        <p>Hey There!</p>
        <p>You can search for things on the internet at:
            <ul>
            <li> <a href="http://www.google.com">Google</a></li>
            <li> <a href="http://www.bing.com">Bing</a></li>
            </ul>
        </p>
    </body>
    </html>

My output:
<html>
    <body>
        <p>Hey There!</p>
        <p>You can search for things on the internet at:
            <ul>
            <li> <a href="http://www.google.com">Google</a></li>
            following line is incorrect. It prints the above line again.
            <li> <a href="http://www.bing.com" href="http://www.google.com">Bing</a></li> 
            </ul>
        </p>
    </body>
    </html>

我的代码:

#include <stdio.h>
#include <stdint.h>
#include <string.h>
void open_tag(char tag[], char attribute[]){
char str1[100];
char str2[100];
char str3[100];
//The bug happens here.The attribute[] is incorrect.
strcpy (str1,tag);
strncat (str1, attribute, 100);
strcpy (str2,"<");
strcpy (str3,">");
strncat (str2, str1, 100);
strncat (str2, str3, 50);
printf ("%s",str2);
}
void close_tag(char tag[]){
char str1[20];
char str2[20];
char str3[20];
strcpy (str1,tag);
strcpy (str2,"</");
strcpy (str3,">");
strncat (str2, str1, 6);
strncat (str2, str3, 6);
printf ("%s",str2);
}

int main(int argc, char *argv[])
{
open_tag("html", "");
printf("\n");
open_tag("body", "");
printf("\n");
open_tag("p", "");
printf("Hey There!");
close_tag("p");
printf("\n");
open_tag("p", "");
printf("You can search for things on the internet at:");
printf("\n");
open_tag("ul", "");
printf("\n");
open_tag("li", "");
char arr2[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','g','o','o','g','l','e','.','c','o','m','\"'};
open_tag("a", arr2);
printf("Google");
close_tag("a");
close_tag("li");
printf("\n");
open_tag("li", "");
char arr1[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','b','i','n','g','.','c','o','m',' ','\"'};  
open_tag("a", arr1);
printf("Bing");
close_tag("a");
close_tag("li");
printf("\n");
close_tag("ul");
printf("\n");
close_tag("p");
printf("\n");
close_tag("body");
printf("\n");
close_tag("html");
printf("\n");
return 0;
}
4

3 回答 3

2

当您创建并传递attribute时,它不是以空值终止的。然后你强迫它复制100个字符(使用strcat),然后你在它的末尾附加字符(<>

arr1我建议你在传递它们之前在你的和字符串上加上一个终止符arr2(通过添加一个'\0'字符)。

然后不要使用strncat(..., 100),只使用普通的strcat()

于 2013-02-11T04:48:52.423 回答
1

这个:

void open_tag(char tag[], char attribute[]){
    char str1[100];
    char str2[100];
    char str3[100];
    //The bug happens here.The attribute[] is incorrect.
    strcpy (str1,tag);
    strncat (str1, attribute, 100);
    strcpy (str2,"<");
    strcpy (str3,">");
    strncat (str2, str1, 100);
    strncat (str2, str3, 50);
    printf ("%s",str2);
}

一定是我见过的最复杂的写这篇文章的方式之一:

void open_tag(char const *tag, char const *attribute)
{
    printf("<%s %s>", tag, attribute);
}

请注意,替换不会对标签或属性的长度施加任意限制,因此它也更可靠(并且更易于阅读)。

close_tag()功能同样怪诞——它可以简化为:

void close_tag(char const *tag)
{
    printf("</%s>", tag);
}

当然,这假设您将以空字符结尾的字符串传递给函数;如果你不这样做,你就什么都没有了。所以,你的代码:

char arr2[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','g','o','o','g','l','e','.','c','o','m','\"'};
open_tag("a", arr2);

应该变得更简洁,例如:

open_tag("a", "href=\"http://www.google.com\"");

文字字符串的常量性使得char const *tag参数是可取的,因为它们没有被修改。

于 2013-02-11T04:54:20.600 回答
0

清理你的代码,

void open_tag(char* tag, char* attribute){

printf("<%s%s>",tag,attribute);

}

void close_tag(char* tag){

printf("</%s>",tag);    

}

int main(int argc, char *argv[])
{
char arr1[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','b','i','n','g','.','c','o','m',' ','\"','\0'};  
char arr2[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','g','o','o','g','l','e','.','c','o','m','\"','\0'};

open_tag("html", "");
printf("\n\t");

open_tag("body", "");
printf("\n\t\t");

open_tag("p", "");
printf("Hey There!");
close_tag("p");
printf("\n\t\t");

open_tag("p", "");
printf("You can search for things on the internet at:\n\t\t\t");
open_tag("ul", "");
printf("\n\t\t\t\t");
open_tag("li", "");

open_tag("a", arr2);
printf("Google");
close_tag("a");
close_tag("li");
printf("\n\t\t\t\t");
open_tag("li", "");
open_tag("a", arr1);
printf("Bing");
close_tag("a");
close_tag("li");
printf("\n\t\t\t");
close_tag("ul");
printf("\n\t\t");
close_tag("p");
printf("\n\t");
close_tag("body");
printf("\n");
close_tag("html");
printf("\n");

return 0;
}

输出:

<html>
    <body>
        <p>Hey There!</p>
        <p>You can search for things on the internet at:
            <ul>
                <li><a href="http://www.google.com">Google</a></li>
                <li><a href="http://www.bing.com ">Bing</a></li>
            </ul>
        </p>
    </body>
</html>

您的代码存在问题:1. 缩进 2. 数组中的空字符 3. 不必要的代码

尽管我清理了您的代码,但您确实意识到这种编码方式并不是真正需要的。

于 2013-02-11T05:21:53.017 回答