修改文件上写入的结构变量
我正在研究这个项目,如果一天和客户在文件上有记录,我需要在该项目中跟踪和修改用户数据消耗。 一切似乎都可以正常工作,客户消耗成功地跟踪了,但是它并没有将其加起来。想知道你们是否可以帮助我找到问题!
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论