在while循环中无限循环 [英] infinite loop in while loop

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

问题描述

fp=fopen("Product.dat","rb+");
while (fread(&prod,sizeof (prod),1,fp)==1) {
    prod.stockquant = prod.stockquant + prod.stockorderquant;
    prod.stockorderquant = 0;
    fseek(fp, -sizeof(prod), SEEK_CUR);
    fwrite (&prod, sizeof(prod), 1, fp);
}
fclose (fp);

一旦进入while循环,我就会陷入无限循环. 文件指针是fp,产品是结构中名为PRODUCT,stockquant和stockorderquant ara变量的Struct的实例.我正在尝试更改stockquant和stockorderquant的值.这是我为我的项目所做的批量更新.我在编辑每个产品的库存数量和订单数量时试图浏览整个名为product.dat的文件.

Once I get into the while loop, I am getting an infinite loop. The file pointer is fp, the prod is an instance of Struct called PRODUCT, stockquant and stockorderquant ara variables in the struct. I am attempting to change the values of stockquant and stockorderquant. This is a batch update tht im doing for my project. I am trying to go through the whole file called product.dat while editing each product's stockquant and orderquant.

为什么会出现无限循环? 当我在用于检查prod.id = userinput是否存在的if语句中使用该方法时,该方法似乎可以工作.

Why am I getting an infinite loop? This method seems to work when I use it in an if statement that checks if the prod.id = userinput or not.

有帮助吗?

一些额外的代码:

void batchupdate(void) { 
    system("cls");
    FILE *fp;
    int c=0;
    gotoxy(20,4);
    printf("****Batch Update Section****");
    char another='y';
    while(another=='y')
    {
        system("cls");
        gotoxy(15,6);
        printf("Are you sure you want to Batch update (Press Y or N)?");

        if((getch()=='y') || (getch() == 'Y')) {
        system("cls");
        int pos;

        fp=fopen("Product.dat","rb+");
        while(fread(&prod,sizeof(prod),1,fp)==1) {
                prod.stockquant = prod.stockquant + prod.stockorderquant;
                product.stockorderquant = 0;

                fseek(fp, -(sizeof(prod)), SEEK_CUR);
                fwrite (&prod, sizeof(prod), 1, fp);
                getchar();
                pos = ftell(fp);
                printf("%d",&pos);


        }
        fclose (fp);

        gotoxy(15,16);
        printf("Complete");
        gotoxy(15,18);
        printf("All products stock quantity have been updated. The stock order quantity has been reset");

        gotoxy(15,16);
        printf("Do you want to modify another product?(Y/N)");
        fflush(stdin);
        another=getch() ; }

    else { if((getch()=='n') || (getch() == 'N')) {
                mainmenu();
           }
    }
}
    returnfunction();
}

这是我列出我的产品的方式(它可以正常工作!)(请不要在此显示的订购数量与stockorderquant无关

This is how I list my products (and it works!) (Please not the order quantity displayed here has nothing to do with the stockorderquant

void listproduct(void)  
{
    int x;
    FILE *fp;
    system("cls");
    gotoxy(1,1);
    printf("*********************************Product List*****************************");
    gotoxy(2,2);
    printf("Name              ID    Price  StockQuant  Order Quant  Description");
    x=4;
    fp=fopen("Product.dat","rb");
    while(fread(&prod,sizeof(prod),1,fp)==1){
        gotoxy(2,x);
        printf("%s",prod.prodname);
        gotoxy(20,x);
        printf("%d",prod.prodid);
        gotoxy(26,x);
        printf("%.2f",prod.price);
        gotoxy(34,x);
        printf("%d",prod.stockquant);
        gotoxy(46,x);
        printf("%d",prod.orderquantity);
        gotoxy(59,x);
        printf("%s",prod.description);
        printf("\n\n");
        x++;
    }
    fclose(fp);
    gotoxy(35,25);
    returnfunction();
}

我的结构定义如下:

struct PRODUCT
{
    int id;
    char name[30];
    char desc[50];
    float price;
    int stockquant;
    int orderquant;
    int stockorderquant;
};

struct PRODUCT prod;

推荐答案

让我引用fopen的手册页:

读取和写入可以以任何顺序混合在读取/写入流上.请注意,除非输入操作遇到文件结尾,否则ANSI C要求在输出和输入之间插入文件定位功能. (如果不满足此条件,则允许读操作返回除最近操作以外的其他写操作的结果.)因此,(fseek(3)或fgetpos(3)操作在这样的流上进行读写操作之间的关系.此操作可能是无操作的操作(如
fseek(..., 0L, SEEK_CUR)需要其同步副作用.

Reads and writes may be intermixed on read/write streams in any order. Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. (If this condition is not met, then a read is allowed to return the result of writes other than the most recent.) Therefore it is good practice (and indeed sometimes necessary under Linux) to put an fseek(3) or fgetpos(3) operation between write and read operations on such a stream. This operation may be an apparent no-op (as in
fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect.

写入后调用fseek

fseek(fp, -sizeof(prod), SEEK_CUR);
fwrite (&prod, sizeof(prod), 1, fp);
fseek(fp, 0, SEEK_CUR);

应该修复它.

这篇关于在while循环中无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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