Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 循环打印仅打印我文件的最后一行(两次循环后)_C - Fatal编程技术网

C 循环打印仅打印我文件的最后一行(两次循环后)

C 循环打印仅打印我文件的最后一行(两次循环后),c,C,我的代码是当用户选择要删除的选项时,显示文件中包含的一些问题。 但我面临这个问题:在de程序显示2个问题之后,它总是只显示我文件的最后一行。 我不知道怎么了 以下是代码(仅限开关菜单的de案例1) printf(“\n所选主题为地质学”); while(continue!=0) { r=0; srand(时间(空)); r=rand()%7; printf(“\n%d”,r); 如果(r==0) r=1; 对于(i=0;i增加了Jonathan Leffler和我之前的评论 每次迭代都会从文件中

我的代码是当用户选择要删除的选项时,显示文件中包含的一些问题。 但我面临这个问题:在de程序显示2个问题之后,它总是只显示我文件的最后一行。 我不知道怎么了

以下是代码(仅限开关菜单的de案例1)

printf(“\n所选主题为地质学”);
while(continue!=0)
{
r=0;
srand(时间(空));
r=rand()%7;
printf(“\n%d”,r);
如果(r==0)
r=1;

对于(i=0;i增加了Jonathan Leffler和我之前的评论

每次迭代都会从文件中读取一些行。第一次迭代可以正常工作,但第二次迭代会从文件的第一个左侧开始,因此您可以快速浏览很多行

需要做的是在每次迭代开始时将文件倒带到开始处。fseek()函数将为您执行此操作。问题是如果
rand(),您还将重复问题%7
再次返回相同的数字。需要一个初始化为全零的静态数组来检查是否已经使用了一个问题。或者使用位图。rewind()是fseek()的一个特例,它返回到文件的开头(想想老式磁带)

另外,
if(arqgeo==NULL)
是错误的,因为apqgeo的值在出现故障时不会改变。请查看ferror()和feof()函数,以测试eof/错误条件。scanf()还返回使用eof转换的字段计数,指示出现这些“f”的某种类型的故障函数会很有用。无论如何,See也返回,
setbuf(stdin,NULL);
只有在流上的任何I/O操作之前执行时才有效。你使用它太晚了,没有任何效果。它也不会给你一个字符一个字符的输入-这需要更多的努力才能实现。使用“continue”作为变量名是一个非常糟糕的主意,因为“continue”在C中是一个关键字。“联系开发人员!!!”你确定这是明智的吗?
            printf("\nThe selected subject was Geology");

                while(continue != 0)
                {
                    r=0;
                    srand(time(NULL));
                    r = rand()%7;
                    printf("\n%d", r);
                    if(r==0)
                        r=1;

                    for(i=0; i<r; i++)
                        fscanf(arqgeo, "%s %s %s %s %s %c", question, alta, altb, altc, altd, &respa);

                    printf("\n\n%s \n%s \n%s \n%s \n%s \n", question, alta, altb, altc, altd);
                    printf("Enter alternative: ");
                    setbuf(stdin,NULL);
                    scanf("%c",&resp);

                    if(arqgeo == NULL)
                    {
                        printf("An error has occurred\n");
                        printf("Contact the developers !!!\n");
                    } else {
                        if(resp == respa)
                        {
                            printf("You're right!!!\n");
                            pont++;
                            printf("\n\n\n");
                    } else {
                        puts("\a");
                        printf("You missed!!!");
                        printf(" The correct answer is: %c", respa);
                        erro++;
                        printf("\n\n\n");
                    }
                    }
                    printf("Do you wish to continue? Enter a number other than 0  ");
                    scanf("%d", &continue);
                    }
                    break;