修改文件上写入的结构变量

发布于 2025-02-08 18:44:22 字数 2190 浏览 2 评论 0原文

我正在研究这个项目,如果一天和客户在文件上有记录,我需要在该项目中跟踪和修改用户数据消耗。 一切似乎都可以正常工作,客户消耗成功地跟踪了,但是它并没有将其加起来。想知道你们是否可以帮助我找到问题!

void CargaArchivoConsumo(char ArchivoConsumo[])
///Add clients struct to file
{
    stConsumo c;
    char opcion;
    int idCon=0, id=1;
    FILE *archi=fopen(ArchivoConsumo,"ab");
    if(archi)
    {
        id=idConsumoUnico("consumo.dat");
        do
        {
            c=cargaConsumo();
            c.id=id;
            idCon=BuscaIDConsumo("consumo.dat",c.fecha,c.idCliente);
///this function finds out if this particular user already has data consumption on this date, in which case it returns that consuption's ID.
            if (idCon!=0)
            {
                CargaConsumoExistente("consumo.dat",idCon,c.datosConsumidos);
///passes repeated record id and new data consumption onto this function, which SHOULD add it up to the previous record. 
                printf("\nYa se cargo un consumo para este usuario en esa fecha. El nuevo consumo se sumara al anterior\n");
            }
            else
            { 
/// If no other record is found, the new struct is written onto the file.
                fwrite(&c,sizeof(stConsumo), 1, archi);
                id++;
            }
            printf("\nPresione ESC para salir, cualquier tecla para continuar");
            fflush(stdin);
            opcion=getch();
            system("cls");
        }
        while(opcion!=ESC);
        fclose(archi);

    }
}

void CargaConsumoExistente(char ArchivoConsumo[],int idCon,int datos) 
///Adds new data on to the old record. Receives record ID and new data as parameter.
{
    stConsumo c;
    int found=0;
    FILE *archi= fopen(ArchivoConsumo, "r+");
    if(archi)
    {
        while (found==0 && fread(&c,sizeof(stConsumo),1,archi)>0)
        {///searching for that specific id on the file, and when found adds the new data onto the record.
            if(idCon==c.id)
            {
                c.datosConsumidos+=datos;           
                fwrite(&c,sizeof(stConsumo),1,archi);
                found=1;
            }
        }
        fclose(archi);
    }
}

一切都按预期工作,后者函数找到了记录,并添加了数据,但没有写入文件。

I'm working on this project, in which I need to track and modify user data consumption if the day and client already have a record on the file.
Everything seems to be working fine, the client consumption is tracked successfully, but it does not add it up to the previous value. Wondering if you guys can help me find the problem!

void CargaArchivoConsumo(char ArchivoConsumo[])
///Add clients struct to file
{
    stConsumo c;
    char opcion;
    int idCon=0, id=1;
    FILE *archi=fopen(ArchivoConsumo,"ab");
    if(archi)
    {
        id=idConsumoUnico("consumo.dat");
        do
        {
            c=cargaConsumo();
            c.id=id;
            idCon=BuscaIDConsumo("consumo.dat",c.fecha,c.idCliente);
///this function finds out if this particular user already has data consumption on this date, in which case it returns that consuption's ID.
            if (idCon!=0)
            {
                CargaConsumoExistente("consumo.dat",idCon,c.datosConsumidos);
///passes repeated record id and new data consumption onto this function, which SHOULD add it up to the previous record. 
                printf("\nYa se cargo un consumo para este usuario en esa fecha. El nuevo consumo se sumara al anterior\n");
            }
            else
            { 
/// If no other record is found, the new struct is written onto the file.
                fwrite(&c,sizeof(stConsumo), 1, archi);
                id++;
            }
            printf("\nPresione ESC para salir, cualquier tecla para continuar");
            fflush(stdin);
            opcion=getch();
            system("cls");
        }
        while(opcion!=ESC);
        fclose(archi);

    }
}

void CargaConsumoExistente(char ArchivoConsumo[],int idCon,int datos) 
///Adds new data on to the old record. Receives record ID and new data as parameter.
{
    stConsumo c;
    int found=0;
    FILE *archi= fopen(ArchivoConsumo, "r+");
    if(archi)
    {
        while (found==0 && fread(&c,sizeof(stConsumo),1,archi)>0)
        {///searching for that specific id on the file, and when found adds the new data onto the record.
            if(idCon==c.id)
            {
                c.datosConsumidos+=datos;           
                fwrite(&c,sizeof(stConsumo),1,archi);
                found=1;
            }
        }
        fclose(archi);
    }
}

Everything is working as expected, the latter function finds the record, and adds the data but it's not written onto the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文