2

我正在用 C 编写一个小程序,用于检查 HTML 文件是否具有正确的打开和关闭标签?但我有一些问题......我有一个文件,其中包含所有可能的标签,名为 tags.txt(这些只是第一个):

<a>
</a>
<abbr>
</abbr>
<area>
</area>
<aside>
</aside>

我有 htmlfile.html,我必须检查:

<!--#echo var="date" -->
<area>
</area>
<area>
</area>

其次,我想将这样的评论替换为 sysdate 之类的,格式可以,我可以做到,但是 prog 将这个文件放入

我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#define MAX_SIZE 512


void menu();
void check();
void datumos();


int main(int argc,char *argv[])
{
    menu();

    return 0;
}

void menu()
{
    char menu[MAX_SIZE];
    while(1 < 2)
    {
            printf("\npress a button:\n\n");

                    printf("\tFile HTML check..............:c\n");
                    printf("\t<!--#echo var="date" -->...........:d\n");
                    printf("\tExit:\tCTRL + C\n");
                    scanf("%s",menu);

            if( strcmp(menu,"c") == 0 )
            {
                    check();
            }
            else if( strcmp(menu,"d") == 0 )
            {
                    datumos();
            }

    }
}

void check()
{
    FILE *htmlfile;
    FILE *checkfile;

    htmlfile = fopen("htmlfile.html","w");
    checkfile = fopen("tags.txt","r");

char line[MAX_SIZE];
char htmlline[MAX_SIZE];
char tags[189][30];


int i=0;

printf("\tcheck__1\n");

while(fgets(line,sizeof(line),checkfile) != NULL)
    {

        int j;
    for(j=0; j<sizeof(line); ++j)
    {
        tags[i][j]=line[j];
    }
    ++i;

    }
printf("\tcheck__2\n");


int k=0;    char htmlfiletags[MAX_SIZE][30];
    while(fgets(htmlline,sizeof(htmlline),htmlfile) != NULL)
    {
    char currentline[sizeof(htmlline)];
    int j=0;

        if( currentline[j]=="<" )
        {

                while(currentline[j]!=">") 
                {
                    htmlfiletags[k][j]=currentline[j];
                    ++j;
                }
                strcat(htmlfiletags[k][j+1],">"); 
                ++k; 
        }
}
printf("\tcheck__3\n");


 int n;
 for(n=0; n<sizeof(htmlfiletags); ++n)
 {
     int j; int howmanytimesnot=0;

     for(j=0; j<sizeof(tags); ++j)
     {
         printf("\tcheck__3/1\n");


         if(strcmp(htmlfiletags[n],tags[j])==0)
         {
             printf("\t%d\n", howmanytimesnot);

             ++howmanytimesnot;
         }
     }

    printf("\tcheck__3/3\n");

     if(!(howmanytimesnot<sizeof(tags)))
        {
            printf("\tcheck__3/4\n");
          printf("the file is not wellformed");

          exit (1);
        }

 }
 printf("\tcheck__4\n");


}

void copy_file(const char *from,const char *to)
{
    FILE *fr;
    FILE *t;
    fr = fopen(from,"r");
    t = fopen(to,"w");

    char line[MAX_SIZE];

    char row[MAX_SIZE];

    while(fgets(line,sizeof(line),fr) != NULL)
    {
            sscanf(line,"%s",row);
            fprintf(t,"%s\n",row);
    }

    fclose(fr);
    fclose(t);

    remove("tempfile.html");
 }


void datumos()
{
time_t now = time(NULL);
struct tm *t = localtime(&now);
char date_time[30];
strftime( date_time, sizeof(date_time), "%x_%X", t );

FILE *htmlfile;
    FILE *tempfile;
    htmlfile = fopen("htmlfile.html","r");
    tempfile = fopen("tempfile.html","w");
    char line[MAX_SIZE];
    //char datecomment[]="<!--#echo var=date -->";

    while(fgets(line,sizeof(line),htmlfile) != NULL)
    {

            if( strcmp(line,"<!--#echo var="date" -->") == 0 )
            {

            char row[40];
            strcpy(row,"<!--");
            strcat(row, date_time);
            strcat(row,"-->");

    printf("%s",row);
            fputs(row,tempfile);

            }
            else
            {
                    fputs(line,tempfile);
            }
    }

    fclose(htmlfile);
    fclose(tempfile);

    copy_file("tempfile.html","htmlfile.html");

}

它死在这里,在内部 for 循环中,在第 200 次检查的 if 中……我不知道为什么……

 int n;
 for(n=0; n<sizeof(htmlfiletags); ++n)
 {
     int j; int howmanytimesnot=0;

     for(j=0; j<sizeof(tags); ++j)
     {
         printf("\tcheck__3/1\n");


         if(strcmp(htmlfiletags[n],tags[j])==0)
         {
             printf("\t%d\n", howmanytimesnot);

             ++howmanytimesnot;
         }
     }

    printf("\tcheck__3/3\n");

     if(!(howmanytimesnot<sizeof(tags)))
        {
            printf("\tcheck__3/4\n");
          printf("the file is not wellformed");

          exit (1);
        }

 }

谢谢大家的回复!!G

4

4 回答 4

1

您的代码非常复杂,它有几个问题。

这是一个:

for(j=0; j<sizeof(tags); ++j)

这不会符合我的预期;sizeof(tags)不是tags(声明为char tags[189][30];)的数组长度,而是变量的总大小。因此,此循环将从 0 变为 189 * 30 - 1,即 5669,因此索引超出数组末尾。

此外,以sizeof任何方式在这里使用的想法都是错误的,因为 的内容tags来自文件,因此编译器不可能知道。请记住sizeof,对于像这样的表达式,它是在编译时评估的。

您需要有一个变量(例如size_t num_tags),为从标记文件解析的每一行增加一个变量,然后您可以使用它来迭代tags.

于 2012-10-24T10:12:17.040 回答
0

不要使用 regex或某种字符串解析来解析 HTML。而是在网络或此站点上搜索 ac 库来解析 html。然后检查已解析的 HTML 文件中的标记。这将大大简化开发,因为您不必自己解析文件。

于 2012-10-24T10:04:54.670 回答
0

我已经修复了一些问题,但是 - 我仍然无法检查文件的 htmltags,死在同一个循环中,我已经修复了标签数组的分配 - 当 htmlfile 中有 2 个或更多不同的评论并且我正在替换评论该程序将其替换为 sysdate,但该程序严重复制了另一个注释,例如 =>

现在的代码是:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>

    #define MAX_SIZE 512


    void menu();
    void check();
    void datumos();


    int main(int argc,char *argv[])
    {
        menu();

        return 0;
    }

    void menu()
    {
        char menu[MAX_SIZE];
        while(1 < 2)
        {
                printf("\npress a button:\n\n");

                        printf("\tFile HTML check..............:c\n");
                        printf("\t<!--#echo var="date" -->...........:d\n");
                        printf("\tExit:\tCTRL + C\n");
                        scanf("%s",menu);

                if( strcmp(menu,"c") == 0 )
                {
                        check();
                }
                else if( strcmp(menu,"d") == 0 )
                {
                        datumos();
                }

        }
    }

    void check()
    {
        FILE *htmlfile;
        FILE *checkfile;

        htmlfile = fopen("htmlfile.html","r");
        checkfile = fopen("tags.txt","r");

        char line[MAX_SIZE];
        char htmlline[MAX_SIZE];


        int i2=0;

        printf("\tcheck__1\n");
        while(fgets(line,sizeof(line),checkfile) != NULL)
        {
            ++i2;

        }


        char tags[i2][20];

        int i=0;

        printf("\tcheck__11\n");
        while(fgets(line,sizeof(line),checkfile) != NULL)
        {
            int j;
            for(j=0; j<sizeof(line); ++j)
            {

                tags[i][j]=line[j];
            }
            ++i;

        }
        printf("\tcheck__2\n");

        int k=0;    char htmlfiletags[MAX_SIZE][30];
        while(fgets(htmlline,sizeof(htmlline),htmlfile) != NULL)
        {
            char currentline[sizeof(htmlline)];
            int j=0;

                if( currentline[j]=="<" )
                {

                        while(currentline[j]!=">")
                        {
                            htmlfiletags[k][j]=currentline[j];
                            ++j;
                        }
                        strcat(htmlfiletags[k][j+1],">");
                        ++k;
                }
        }
        printf("\tcheck__3\n");

         int n;
         for(n=0; n<sizeof(htmlfiletags); ++n)
         {
             int j; int howmanytimesnot=0;

             for(j=0; j<sizeof(tags); ++j)
             {
                 //printf("\tcheck__3/1\n");

                 if(strcmp(htmlfiletags[n],tags[j])==0)
                 {
                   //  printf("\t%d\n", howmanytimesnot);
                     ++howmanytimesnot;
                 }
             }

            printf("\tcheck__3/3\n");

             if(!(howmanytimesnot<sizeof(tags)))
                {
                    printf("\tcheck__3/4\n");
                  printf("the file is not wellformed");

                  exit (1);
                }

         }
         printf("\tcheck__4\n");

    }

    void copy_file(const char *from,const char *to)
    {
        FILE *fr;
        FILE *t;
        fr = fopen(from,"r");
        t = fopen(to,"w");

        char line[MAX_SIZE];
        char row[MAX_SIZE];

        while(fgets(line,sizeof(line),fr) != NULL)
        {
            sscanf(line,"%s",row);
            fprintf(t,"%s\n",row);
        }

        fclose(fr);
        fclose(t);

        remove("tempfile.html");
    }


    void datumos()
    {
    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    char date_time[30];
    strftime( date_time, sizeof(date_time), "%x_%X", t );

        FILE *htmlfile;
        FILE *tempfile;

        htmlfile = fopen("htmlfile.html","r");
        tempfile = fopen("tempfile.html","w");

        char line[MAX_SIZE];
        char* datecomment="<!--#echo var=\"date\" -->";

        while(fgets(line,sizeof(line),htmlfile) != NULL)
        {

            int i3; int db=0;
            for(i3=0; i3<strlen(datecomment); ++i3)
            {
                if(line[i3]==datecomment[i3])
                {
                    ++db;
                }

            }

            if(db==strlen(datecomment))
            {

            char row[30];
            strcpy(row,"<!--");
            strcat(row, date_time);
            strcat(row,"-->\n");

            fputs(row,tempfile);

            }
            else
            {
                fputs(line,tempfile);
            }

        }

        fclose(htmlfile);
        fclose(tempfile);

        copy_file("tempfile.html","htmlfile.html");

    }
于 2012-10-25T06:37:51.613 回答
0
  • 当前行没有必要,我也修复了比较

    while(fgets(htmlline,sizeof(htmlline),htmlfile) != NULL)
    {           
        int j=0;
    
            if( htmlline[j]=='<' )
            {
    
                    while(htmlline[j]!='>')
                    {
                        htmlfiletags[k][j]=htmlline[j];
                        ++j;
                    }
                    strcat(htmlfiletags[k][j+1],">");
                    ++k;
            }
    }
    

-另外,另一个问题是只替换合适的评论,不要伤害不同的评论仍然不起作用

“所以它替换

  <!--#echo var="date" --> to the sysdate, it's ok, but when there are different comments like

  <!--#include something -->, it wont be copied back well, in the htmlfile will be only <!--#include"

想法?

于 2012-10-25T20:47:05.867 回答