如何重置一个while循环? [英] How do I reset a while loop?

查看:271
本文介绍了如何重置一个while循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的工作的一些code的提取物。只是想知道我将如何去重置嵌套循环?它保持在该文件的末尾时,第一环路再次运行。

 而(的fscanf(FP2,%S,wordcheck)!= EOF)//读取从文本文件单词到数组//
    {        对于(i = 0; wordcheck [I];我++)
        {
            wordcheck [I] = tolower的(wordcheck [I]); //使所有字符小写//
        }        的printf(%S,wordcheck);        而(的fscanf(FP1,%S,worddict))
        {
            如果(STRCMP(wordcheck,worddict)== 0)//比较字符串//
            {
            的printf(这个词:%s是在字典中的\\ n,wordcheck);
            dictcount ++;
            打破;
            }            其他
            {
            dictcount ++;
            }            如果(worddict == NULL)
            {
            的printf(你的话:%s不是在字典中的\\ n,wordcheck);
            }
        }
    }


解决方案

使用 fseek的

  fseek的(FP2,0,SEEK_SET);

..或者更简单地说, 退

 退(FP2);

Here is an extract for some code that I am working on. Just wondering how would I go about resetting the nested loop? It remains at the end of the file when the first loop runs again.

while(fscanf(fp2,"%s", wordcheck)!=EOF)//Reads word from text file into array//
    {

        for (i=0; wordcheck[i]; i++)
        {
            wordcheck[i] = tolower(wordcheck[i]);//makes all characters lower case//
        }

        printf("%s", wordcheck);

        while(fscanf(fp1,"%s", worddict))
        {   
            if(strcmp(wordcheck, worddict)==0)//compare strings//
            {
            printf("This word: %s is in the dictionary\n", wordcheck);
            dictcount++;
            break;
            }

            else
            {
            dictcount++;
            }

            if(worddict == NULL)
            {
            printf("Your word: %s is not in the dictionary\n", wordcheck);
            }
        }
    }

解决方案

Use fseek:

fseek(fp2, 0, SEEK_SET);

.. or more simply, rewind:

rewind(fp2);

这篇关于如何重置一个while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆