C 使用指针查找文件中的标记

C 使用指针查找文件中的标记,c,arrays,pointers,C,Arrays,Pointers,所以我对使用指针很陌生,所以我尝试的东西可能根本没有意义。。。?我正在从事一个项目,该项目要求我通过搜索标记的起点和终点(从文件中读取)来查找文件中的html标记 目前,我已将整个文件写入一个一维字符数组中,我必须严格使用指针在数组中搜索以标识符“”开头和结尾的任何标记。如果标识符之间有一个“/”或空白,那么它不是一个标记,我们不需要保留它的引用。数组的设置方式使其保留初始文件的格式 一旦找到一个标记,我们必须在数组中搜索匹配项,并计算有多少个相同的标记。我被限制在数组中存储找到的标记,但我能够

所以我对使用指针很陌生,所以我尝试的东西可能根本没有意义。。。?我正在从事一个项目,该项目要求我通过搜索标记的起点和终点(从文件中读取)来查找文件中的html标记

目前,我已将整个文件写入一个一维字符数组中,我必须严格使用指针在数组中搜索以标识符“”开头和结尾的任何标记。如果标识符之间有一个“/”或空白,那么它不是一个标记,我们不需要保留它的引用。数组的设置方式使其保留初始文件的格式

一旦找到一个标记,我们必须在数组中搜索匹配项,并计算有多少个相同的标记。我被限制在数组中存储找到的标记,但我能够在指针数组中存储指向每个标记的开始和结束的指针。任何指导都将不胜感激。这是我的代码,它有可能工作,但我想得到一些实践与指针

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

int main(int argc, char **argv)
{
    FILE *fptr;
    char maxLength[10] = "";// this is where each tag less than 10 characters long will be held
    char htags[100][10]; // Array that will be used to store each tag found
    int tagCounts[100]; // Array that will count the amount of times a tag shows up
    for (int i = 0; i < 100; i++)
        tagCounts[i] = 0;
    int n = 0;
    char *filename = argv[1]; // Input file to be read
    fptr = fopen(filename, "r"); // Read through the file
    if (fptr == NULL)
    {
        printf("File not found...");
        return 0;
    }
int c;
    if (fptr != NULL)
    {
        while (c = fgetc(fptr) != NULL)// making sure we are reading until the very end of the file
        {
            char c = fgetc(fptr); // reading character by character

            if (c == '<' && !feof(fptr))// searching for tags that start with '<'
            {
                char ch = fgetc(fptr);
                if (ch != '/') // checking to see if it is actually a tag
                {
                    int i = 0;
                    strcpy(maxLength, ""); // this is what we will use to store the tags when we find one
                    while (ch != 10 && ch != '>' && ch != ' ')
                    {
                        maxLength[i] = ch; // adds the next character to str[] until
                        // it gets to the end of the tag
                        ch = fgetc(fptr);
                        i++; // increment i so that the characters don't overlap in the array
                    }

                    maxLength[i] = '\0'; // Used to indicate the termination of the character string for a tag

                    int number = 1;

                    for (int i = 0; i < n; i++)// Checking to see if the tag has already
                                               // been found before in the file
                    {
                        if (strcmp(htags[i], maxLength) == 0) // tag has been found before
                        {
                            tagCounts[i]++; // increase the count by 1
                            number = 0;
                            break;
                        }
                    }

                    if (number == 1)
                    {
                        strcpy(htags[n], maxLength); // store the newly found tag in the array
                        tagCounts[n]++;
                        n++; // A new tag has been found, so we must create another element in the array and subsequently increase it's count
                    }
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        printf("Tag-> %s -> Appeared %d time(s)\n", htags[i], tagCounts[i]);
    }
}
这是我能想到的,但我对材料的理解遇到了障碍

    char *contents = buffer;    

    if(buffer != NULL){
       char *c = contents;

        if(*c == '<'){
            char *ch = contents;

            if(*ch != '/'){
                while(*ch != '\0' && *ch != '>' && *ch != ' '){
                    contents++; //nothing is telling us it isn't a tag, so find the end
                    //found the tag, so get the location right before it?
                    if(*ch == '>'){
                        *sTags = contents--;
                    }
                    *sTags = '\0';
                }
                //This is where I am confused.
                //How would I go about comparing the string to other strings
                //throughout the file if it is not stored somewhere..?
            }
        }
    }
    printf("%s", sTags);
char*内容=缓冲区;
if(缓冲区!=NULL){
char*c=内容;
如果(*c=''&&*ch!=''){
contents++;//没有任何东西告诉我们它不是标记,所以请找到结尾
//找到了标签,所以在它之前找到位置?
如果(*ch='>')){
*sTags=内容--;
}
*sTags='\0';
}
//这就是我困惑的地方。
//如何将该字符串与其他字符串进行比较
//如果文件未存储在某个位置,则在整个文件中。。?
}
}
}
printf(“%s”,sTags);

>p>两个要考虑的事项。

1) isspace(c) 您可能希望使用isspace(c)而不是c=''。

因为检查(c='')似乎会允许标记跨越行的两端。 例如,对于这样一个四行文件,您的代码会做什么

<alpha>
<be
ta>
<gamma>

如果有标记的开始和结束指针,则长度为(end beg+1)

因此,在第一种longish main()方法中,您将遍历已知的标记(如果有的话),以检查是否匹配

基于指针的标记可以在已知标记上循环,您只需调用strncmp() 如果候选标记的长度与已知标记的长度相同

最后 最后,对于printf()调试,我发现这些在我的代码中非常有用。 别犹豫,把东西打印出来

如果您还没有用于调试的小测试文件,我建议您制作一个


祝你好运

底部的printf只是为了看看我是否从中得到了什么注意,
main()
中的代码格式非常糟糕。我已将其重新格式化为我的首选约定-只要结果可识别,您可以将其格式化为首选约定。此代码
int tagCounts[100];/*数组,该数组将计算标记出现的次数*/for(inti=0;i<100;i++)tagCounts[i]=0可以替换为
int标记计数[100]={0};/*数组,它将计算标记出现的次数*/
。注意,我不知道这是怎么回事!feof()已经开始工作了,现在已经远离编码好几年了。只是跳回C,还有很多东西要学。嗯……看来我前面的工作比我预期的要多得多。至于不存储副本,只是为了习惯指针的行为,但第一个指针确实能够捕获所有内容。假设它们少于10个字符,则文件是以这种方式编写的。我还没有考虑到这种情况。。。我认为这不是自动取款机。。但只是使用指针来更好地理解它们实际上是如何工作的。我会着手做推荐的!隐马尔可夫模型。。。如果可以修改缓冲区,可以覆盖“…”等标记中的“>”并将其更改为“…”。。。
<alpha>
<be
ta>
<gamma>