简体   繁体   English

我将如何使用 strtok 逐字比较

[英]How would i Use strtok to compare word by word

I've been reading up on strtok and thought it would be the best way for me to compare two files word by word.我一直在阅读 strtok 并认为这将是我逐字比较两个文件的最佳方式。 So far i can't really figure out how i would do it though到目前为止,我真的不知道我会怎么做

Here is my function that perfoms it:这是我的 function 执行它:

int wordcmp(FILE *fp1, FILE *fp2)
{
   char *s1;
   char *s2;
   char *tok;
   char *tok2;
   char line[BUFSIZE];
   char line2[BUFSIZE];
   char comp1[BUFSIZE];
   char comp2[BUFSIZE];
   char temp[BUFSIZE];
   int word = 1;
   size_t i = 0;

while((s1 = fgets(line,BUFSIZE, fp1)) && (s2 = fgets(line2,BUFSIZE, fp2)))
{
    ;
}

tok = strtok(line, " ");
tok2 = strtok(line, " ");

while(tok != NULL)
{
    tok = strtok (NULL, " ");

}


return 0;
}

Don't mind the unused variables, I've been at this for 3 hours and have tried all possible ways I can think of to compare the values of the first and second strtok.不要介意未使用的变量,我已经在此工作了 3 个小时,并尝试了所有可能的方法来比较第一个和第二个 strtok 的值。 Also I would to know how i would check which file reaches EOF first.另外我想知道我将如何检查哪个文件首先到达 EOF。

when i tried当我尝试

  if(s1 == EOF && s2 != EOF)
  {
      return -1;
  }

It returns -1 even when the files are the same?即使文件相同,它也会返回-1? Is it because in order for it to reach the if statement outside of the loop both files have reached EOF which makes the program always go to this if statement?是不是因为为了让它到达循环外的 if 语句,两个文件都达到了 EOF,这使得程序总是 go 到这个 if 语句?

Thanks in advance!提前致谢!

If you want to check if files are same try doing,如果您想检查文件是否相同,请尝试执行,

    do {
       s1 = fgetc(fp1);
       s2 = fgetc(fp2);

       if (s1 == s2) {
            if (s1 == EOF) {
                return 1; // RETURN TRUE
            }
            continue;
      }
      else {
        return -1;  // RETURN FALSE
      }

  } while (1);

Good Luck:)祝你好运:)

When you use strtok() you typically use code like this:当您使用 strtok() 时,您通常使用如下代码:

tok = strtok(line, " ");
while (NULL != tok)
{
    tok = strtok(NULL, " ");
}

The NULL in the call in the loop tells strtok to continue from after the previously found token until it finds the null terminating character in the value you originally passed (line) or until there are no more tokens.循环调用中的 NULL 告诉 strtok 从先前找到的令牌之后继续,直到它在您最初传递的值中找到 null 终止字符(行)或直到没有更多的令牌。 The current pointer is stored in the run time library, and once strtok() returns NULL to indicate no more tokens any more calls to strtok() using NULL as the first parameter (to continue) will result in NULL.当前指针存储在运行时库中,一旦 strtok() 返回 NULL 以指示不再有令牌调用 strtok() 使用 NULL 作为第一个参数(继续)将导致 Z6C3E22344D48924EC210 You need to call it with another value (eg another call to strtok(line, " ")) to get it to start again.您需要用另一个值调用它(例如,另一个调用 strtok(line, " "))让它重新开始。

What this means is that to use strtok on two different strings at the same time you need to manually update the string position and pass in a modified value on each call.这意味着要同时在两个不同的字符串上使用 strtok,您需要手动更新字符串 position 并在每次调用时传入修改后的值。

tok = strtok(line, " ");
tok2 = strtok(line2, " ");
while (NULL != tok && NULL != tok2)
{
    /* Do stuff with tok and tok2 here */
    if (strcmp(tok, tok2)... {}
    /* Update strtok pointers */
    tok += strlen(tok) + 1;
    tok2 += strlen(tok2) + 1;
    /* Get next token */
    tok = strtok(tok, " ");
    tok2 = strtok(tok2, " ");
}

You'll still need to add logic for determining whether lines are different - you've not said whether the files are equivalent if a line break occurs at different position but the words surrounding it are the same.您仍然需要添加逻辑来确定行是否不同 - 如果换行符发生在不同的 position 但围绕它的单词是相同的,您还没有说文件是否等效。 I assume it should be, given your description, but it makes the logic more awkward as you only need to perform the initial fgets() and strtok() for a file if you don't already have a token.鉴于您的描述,我认为应该是这样,但这会使逻辑更加尴尬,因为如果您还没有令牌,则只需对文件执行初始 fgets() 和 strtok() 即可。 You also need to look at how files are read in. Currently your first while loop just reads lines until the end of the file without processing them.您还需要查看文件是如何读入的。目前,您的第一个 while 循环只读取行直到文件末尾而不处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM