Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.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_File_Project - Fatal编程技术网

C 倒带第二次打印文件元素

C 倒带第二次打印文件元素,c,file,project,C,File,Project,我正在尝试一个C语言程序,但面临一些困难。 问题:我在项目中使用文件,但当我倒回文件1时,它会在文件2中复制相同的数据。我怎样才能解决这个问题 您可以参考下面的代码 void calculate_bill () { system("cls"); int quan,q; printf("Enter the number of item : "); scanf("%d",&quan); char sell[100]; float total=0

我正在尝试一个C语言程序,但面临一些困难。 问题:我在项目中使用文件,但当我倒回文件1时,它会在文件2中复制相同的数据。我怎样才能解决这个问题

您可以参考下面的代码

void calculate_bill ()
{
    system("cls");
    int quan,q;
    printf("Enter the number of item : ");
    scanf("%d",&quan);
    char sell[100];
    float total=0,gtotal=0;
    file1=fopen("object.txt","r");
    file2=fopen("temp.txt","w");
    //printf("SN\tITEM NAME\tCODE\tRATE\t\tQUANTITY\tTOTAL\n");
    rewind(file1);
    for(int i=1;i<=quan;i++)
    {
        rewind(file1);
        printf("Enter the item name : ");
        scanf("%s",&sell);
        printf("Enter the item quantity : ");
        scanf("%d",&q);
        while(fscanf(file1,"%s %d %f %d",&object.name,&object.code,&object.rate,&object.quantity)!=EOF)
        {
            if(strcmp(sell,object.name)==0)
            {
            total=object.rate*q;
            gtotal=gtotal+total;
            object.quantity=object.quantity-q;
            fprintf(file2,"%s\t\t%d\t%f\t%d\n",object.name,object.code,object.rate,object.quantity);
            }
            else
            fprintf(file2,"%s\t\t%d\t%f\t%d\n",object.name,object.code,object.rate,object.quantity);
        }
    }
    fclose(file1);
    fclose(file2);
    remove("object.txt");
    rename("temp.txt","object.txt");
    printf("The total amount is : %.2f",gtotal);
    getch();
    system("cls");
    d_mainmenu();
}
我需要这样的输出

SN      ITEM NAME       CODE    RATE            QUANTITY
1       abed            105     100.000000      7

2       maruf           100     100.000000      9

3       adi             106     100.000000      10

4       anik            89      1.000000        1
使用此功能后

。。。但当我倒带我的文件1时,它会在文件2中复制相同的数据

嗯,复制数据的不是倒带。在中,您可以打印从文件1到文件2结尾的每一行。因此,如果for循环执行3次,file2将包含file1内容的3倍,并修改一些行

修复现有代码的最简单方法可能是停止使用rewind,而是在for循环中执行打开、关闭和重命名

比如:


<>你说,你应该考虑一个新的设计,你首先把文件FIL1读入内存,也就是把你的对象类型数组。然后修改数组元素的值,最后将值写回文件。

每次输入内容时,都会将所有条目追加到输出中。我想你最好先把你所有的数据读入一个合适的结构,然后修改数据,然后在处理完之后再写更新后的数据。cna先生,你给我一个示例代码吗?你的期望是什么?是否要减少object.file中的对象数量?
SN      ITEM NAME       CODE    RATE            QUANTITY
1       abed            105     100.000000      7

2       maruf           100     100.000000      9

3       adi             106     100.000000      10

4       anik            89      1.000000        1
for(int i=1;i<=quan;i++)
{
    file1=fopen("object.txt","r");
    file2=fopen("temp.txt","w");

    printf("Enter the item name : ");
    ...
    ...
    while(...)
    {
        ...
    }

    fclose(file1);
    fclose(file2);
    remove("object.txt");
    rename("temp.txt","object.txt");
}