如何重置while循环?(How do I reset a while loop?)

编程入门 行业动态 更新时间:2024-10-26 12:27:00
如何重置while循环?(How do I reset a while loop?)

这是我正在处理的一些代码的摘录。 只是想知道如何重置嵌套循环? 当第一个循环再次运行时,它仍保留在文件的末尾。

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); } } }

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); } } }

最满意答案

使用fseek :

fseek(fp2, 0, SEEK_SET);

..或更简单地说, rewind :

rewind(fp2);

Use fseek:

fseek(fp2, 0, SEEK_SET);

.. or more simply, rewind:

rewind(fp2);

更多推荐

本文发布于:2023-08-07 21:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:reset   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!