Z
zbenta
Hi you guys.
I need some help with a program I've written.
I can not seam to find the reason for the error that ocurrs.
But let me explain.
I have a txt file that works as a database to store information about
materials, whenever I use the function that I've written to edit the
info on that line I get a strange error.
For example:
I have the following line "tt 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
if i try to edit any of the numbers and change them to a two decimal
house number the number following the deleted one disapears.
Following is the code i use to do it, can anyone see what the problem
is.
void editData(char *filename,char *nameval)
{
FILE *fp;
FILE *fp2;
int count=0;
char inputString[MAXSTRING];
int i=0;
int option;
char seps[]=" ";
char *tempString;
int flag20=0;
char
*name,*tref,*vref,*pref,*alfas,*alfam,*bs,*bm,*b5,*b6,*b1m,*b2m,*b3m,*b4m,*b1s,*b2s,*b3s,*b4s,*b7,*b8,*b9;
char outputString[MAXSTRING];
//CHECKS IF THE ORIGINAL FILE CAN BE OPENED
if((fp=fopen(filename,"r"))==NULL)
{
printf("Error opening the file %s",fp);
}
//CHECKS IF THE TMP FILE CAN BE OPENED
if((fp2=fopen("insert.txt","w"))==NULL)
{
printf("The insert.txt file could not be created");
}
//CREATION OF HEADER IN THE TMP FILE
fprintf(fp2,"material_name tref vref pref alfas alfam bs bm b5 b6 b1m
b2m b3m b4m b1s b2s b3s b4s b7 b8 b9 \n");
//READS EACH LINE IN THE FILE
while(fgets(inputString, 2048, fp)!=NULL)
{
i++;
//SKIPS THE HEADER
if(i>1)
{//MAKES THE ATRIBUTION OF EACH VALUE TO IT'S RESPECTIVE VARIABLE
tempString=strtok(inputString,seps);
name=tempString;
tempString = strtok(NULL, seps);
tref=tempString;
tempString = strtok(NULL, seps);
vref=tempString;
tempString = strtok(NULL, seps);
pref=tempString;
tempString = strtok(NULL, seps);
alfas=tempString;
tempString = strtok(NULL, seps);
alfam=tempString;
tempString = strtok(NULL, seps);
bs=tempString;
tempString = strtok(NULL, seps);
bm=tempString;
tempString = strtok(NULL, seps);
b5=tempString;
tempString = strtok(NULL, seps);
b6=tempString;
tempString = strtok(NULL, seps);
b1m=tempString;
tempString = strtok(NULL, seps);
b2m=tempString;
tempString = strtok(NULL, seps);
b3m=tempString;
tempString = strtok(NULL, seps);
b4m=tempString;
tempString = strtok(NULL, seps);
b1s=tempString;
tempString = strtok(NULL, seps);
b2s=tempString;
tempString = strtok(NULL, seps);
b3s=tempString;
tempString = strtok(NULL, seps);
b4s=tempString;
tempString = strtok(NULL, seps);
b7=tempString;
tempString = strtok(NULL, seps);
b8=tempString;
tempString = strtok(NULL, seps);
b9=tempString;
//CHECKS IF THE VALUE IS THE CORRECT ONE
if(strcmp(name,nameval)==0)
{
//PRINTS THE INFO IN THE SCREEN
printf("this are the values to be edited %s %s %s %s %s %s %s
%s %s %s %s %s %s %s %s %s %s %s %s %s %s
\n\n",name,tref,vref,pref,alfas,alfam,bs,bm,b5,b6,b1m,b2m,b3m,b4m,b1s,b2s,b3s,b4s,b7,b8,b9);
count++;
do
{//SWITCHES THROUGH THE OPTIONS
printf("\n\tWich value would you like to change\n");
printf("\t1-tref:\n");
printf("\t2-vref:\n");
printf("\t3-pref:\n");
printf("\t4-alfas:\n");
printf("\t5-alfam:\n");
printf("\t6-bs:\n");
printf("\t7-bm:\n");
printf("\t8-b5:\n");
printf("\t9-b6:\n");
printf("\t10-b1m:\n");
printf("\t11-b2m:\n");
printf("\t12-b3m:\n");
printf("\t13-b4m:\n");
printf("\t14-b1s:\n");
printf("\t15-b2s:\n");
printf("\t16-b3s:\n");
printf("\t17-b4s:\n");
printf("\t18-b7:\n");
printf("\t19-b8:\n");
printf("\t20-b9:\n");
printf("\t0-Exit:\n");
printf("\tYour option:");
scanf("\t%d",&option);
switch(option)
{
case 1:
printf("Insert new value for tref: ");
scanf("%s",tref);
break;
case 2:
printf("Insert new value for vref: ");
scanf("%s",vref);
break;
case 3:
printf("Insert new value for pref: ");
scanf("%s",pref);
break;
case 4:
printf("Insert new value for alfas: ");
scanf("%s",alfas);
break;
case 5:
printf("Insert new value for alfam: ");
scanf("%s",alfam);
break;
case 6:
printf("Insert new value for bs: ");
scanf("%s",bs);
break;
case 7:
printf("Insert new value for bm: ");
scanf("%s",bm);
break;
case 8:
printf("Insert new value for b5: ");
scanf("%s",b5);
break;
case 9:
printf("Insert new value for b6: ");
scanf("%s",b6);
break;
case 10:
printf("Insert new value for b1m: ");
scanf("%s",b1m);
break;
case 11:
printf("Insert new value for b2m: ");
scanf("%s",b2m);
break;
case 12:
printf("Insert new value for b3m: ");
scanf("%s",b3m);
break;
case 13:
printf("Insert new value for b4m: ");
scanf("%s",b4m);
break;
case 14:
printf("Insert new value for b1s: ");
scanf("%s",b1s);
break;
case 15:
printf("Insert new value for b2s: ");
scanf("%s",b2s);
break;
case 16:
printf("Insert new value for b3s: ");
scanf("%s",b3s);
break;
case 17:
printf("Insert new value for b4s: ");
scanf("%s",b4s);
break;
case 18:
printf("Insert new value for b7: ");
scanf("%s",b7);
break;
case 19:
printf("Insert new value for b8: ");
scanf("%s",b8);
break;
case 20:
printf("Insert new value for b9: ");
flag20=1;
scanf("%s",b9);
break;
case 0:
break;
}
}while(option!=0);
//WRITES TO THE TMP FILE THE EDITED VALUES
if (flag20==1)
{
fprintf(fp2,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
%s %s %s %s %s
%s\n",name,tref,vref,pref,alfas,alfam,bs,bm,b5,b6,b1m,b2m,b3m,b4m,b1s,b2s,b3s,b4s,b7,b8,b9);
}
else
{
fprintf(fp2,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
%s %s %s %s %s
%s",name,tref,vref,pref,alfas,alfam,bs,bm,b5,b6,b1m,b2m,b3m,b4m,b1s,b2s,b3s,b4s,b7,b8,b9);
}
}
else
{
//WRITES TO THE TMP FILE THE VALUES FROM THE ORIGINAL FILE
fprintf(fp2,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
%s %s %s %s
%s",name,tref,vref,pref,alfas,alfam,bs,bm,b5,b6,b1m,b2m,b3m,b4m,b1s,b2s,b3s,b4s,b7,b8,b9);
}
}
}
fclose(fp);
fclose(fp2);
//NOW THE TMP FILE WILL BE RENAMED IN ORDER TO TAKE THE PLACE OF THE
ORIGINAL FILE
rename("insert.txt","materials3.txt");
rename("materials.txt","materialsi.tmp");
rename("materials3.txt","materials.txt");
if(count==0)
{
printf("\nValue not Found\n");
}
}
I need some help with a program I've written.
I can not seam to find the reason for the error that ocurrs.
But let me explain.
I have a txt file that works as a database to store information about
materials, whenever I use the function that I've written to edit the
info on that line I get a strange error.
For example:
I have the following line "tt 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
if i try to edit any of the numbers and change them to a two decimal
house number the number following the deleted one disapears.
Following is the code i use to do it, can anyone see what the problem
is.
void editData(char *filename,char *nameval)
{
FILE *fp;
FILE *fp2;
int count=0;
char inputString[MAXSTRING];
int i=0;
int option;
char seps[]=" ";
char *tempString;
int flag20=0;
char
*name,*tref,*vref,*pref,*alfas,*alfam,*bs,*bm,*b5,*b6,*b1m,*b2m,*b3m,*b4m,*b1s,*b2s,*b3s,*b4s,*b7,*b8,*b9;
char outputString[MAXSTRING];
//CHECKS IF THE ORIGINAL FILE CAN BE OPENED
if((fp=fopen(filename,"r"))==NULL)
{
printf("Error opening the file %s",fp);
}
//CHECKS IF THE TMP FILE CAN BE OPENED
if((fp2=fopen("insert.txt","w"))==NULL)
{
printf("The insert.txt file could not be created");
}
//CREATION OF HEADER IN THE TMP FILE
fprintf(fp2,"material_name tref vref pref alfas alfam bs bm b5 b6 b1m
b2m b3m b4m b1s b2s b3s b4s b7 b8 b9 \n");
//READS EACH LINE IN THE FILE
while(fgets(inputString, 2048, fp)!=NULL)
{
i++;
//SKIPS THE HEADER
if(i>1)
{//MAKES THE ATRIBUTION OF EACH VALUE TO IT'S RESPECTIVE VARIABLE
tempString=strtok(inputString,seps);
name=tempString;
tempString = strtok(NULL, seps);
tref=tempString;
tempString = strtok(NULL, seps);
vref=tempString;
tempString = strtok(NULL, seps);
pref=tempString;
tempString = strtok(NULL, seps);
alfas=tempString;
tempString = strtok(NULL, seps);
alfam=tempString;
tempString = strtok(NULL, seps);
bs=tempString;
tempString = strtok(NULL, seps);
bm=tempString;
tempString = strtok(NULL, seps);
b5=tempString;
tempString = strtok(NULL, seps);
b6=tempString;
tempString = strtok(NULL, seps);
b1m=tempString;
tempString = strtok(NULL, seps);
b2m=tempString;
tempString = strtok(NULL, seps);
b3m=tempString;
tempString = strtok(NULL, seps);
b4m=tempString;
tempString = strtok(NULL, seps);
b1s=tempString;
tempString = strtok(NULL, seps);
b2s=tempString;
tempString = strtok(NULL, seps);
b3s=tempString;
tempString = strtok(NULL, seps);
b4s=tempString;
tempString = strtok(NULL, seps);
b7=tempString;
tempString = strtok(NULL, seps);
b8=tempString;
tempString = strtok(NULL, seps);
b9=tempString;
//CHECKS IF THE VALUE IS THE CORRECT ONE
if(strcmp(name,nameval)==0)
{
//PRINTS THE INFO IN THE SCREEN
printf("this are the values to be edited %s %s %s %s %s %s %s
%s %s %s %s %s %s %s %s %s %s %s %s %s %s
\n\n",name,tref,vref,pref,alfas,alfam,bs,bm,b5,b6,b1m,b2m,b3m,b4m,b1s,b2s,b3s,b4s,b7,b8,b9);
count++;
do
{//SWITCHES THROUGH THE OPTIONS
printf("\n\tWich value would you like to change\n");
printf("\t1-tref:\n");
printf("\t2-vref:\n");
printf("\t3-pref:\n");
printf("\t4-alfas:\n");
printf("\t5-alfam:\n");
printf("\t6-bs:\n");
printf("\t7-bm:\n");
printf("\t8-b5:\n");
printf("\t9-b6:\n");
printf("\t10-b1m:\n");
printf("\t11-b2m:\n");
printf("\t12-b3m:\n");
printf("\t13-b4m:\n");
printf("\t14-b1s:\n");
printf("\t15-b2s:\n");
printf("\t16-b3s:\n");
printf("\t17-b4s:\n");
printf("\t18-b7:\n");
printf("\t19-b8:\n");
printf("\t20-b9:\n");
printf("\t0-Exit:\n");
printf("\tYour option:");
scanf("\t%d",&option);
switch(option)
{
case 1:
printf("Insert new value for tref: ");
scanf("%s",tref);
break;
case 2:
printf("Insert new value for vref: ");
scanf("%s",vref);
break;
case 3:
printf("Insert new value for pref: ");
scanf("%s",pref);
break;
case 4:
printf("Insert new value for alfas: ");
scanf("%s",alfas);
break;
case 5:
printf("Insert new value for alfam: ");
scanf("%s",alfam);
break;
case 6:
printf("Insert new value for bs: ");
scanf("%s",bs);
break;
case 7:
printf("Insert new value for bm: ");
scanf("%s",bm);
break;
case 8:
printf("Insert new value for b5: ");
scanf("%s",b5);
break;
case 9:
printf("Insert new value for b6: ");
scanf("%s",b6);
break;
case 10:
printf("Insert new value for b1m: ");
scanf("%s",b1m);
break;
case 11:
printf("Insert new value for b2m: ");
scanf("%s",b2m);
break;
case 12:
printf("Insert new value for b3m: ");
scanf("%s",b3m);
break;
case 13:
printf("Insert new value for b4m: ");
scanf("%s",b4m);
break;
case 14:
printf("Insert new value for b1s: ");
scanf("%s",b1s);
break;
case 15:
printf("Insert new value for b2s: ");
scanf("%s",b2s);
break;
case 16:
printf("Insert new value for b3s: ");
scanf("%s",b3s);
break;
case 17:
printf("Insert new value for b4s: ");
scanf("%s",b4s);
break;
case 18:
printf("Insert new value for b7: ");
scanf("%s",b7);
break;
case 19:
printf("Insert new value for b8: ");
scanf("%s",b8);
break;
case 20:
printf("Insert new value for b9: ");
flag20=1;
scanf("%s",b9);
break;
case 0:
break;
}
}while(option!=0);
//WRITES TO THE TMP FILE THE EDITED VALUES
if (flag20==1)
{
fprintf(fp2,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
%s %s %s %s %s
%s\n",name,tref,vref,pref,alfas,alfam,bs,bm,b5,b6,b1m,b2m,b3m,b4m,b1s,b2s,b3s,b4s,b7,b8,b9);
}
else
{
fprintf(fp2,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
%s %s %s %s %s
%s",name,tref,vref,pref,alfas,alfam,bs,bm,b5,b6,b1m,b2m,b3m,b4m,b1s,b2s,b3s,b4s,b7,b8,b9);
}
}
else
{
//WRITES TO THE TMP FILE THE VALUES FROM THE ORIGINAL FILE
fprintf(fp2,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
%s %s %s %s
%s",name,tref,vref,pref,alfas,alfam,bs,bm,b5,b6,b1m,b2m,b3m,b4m,b1s,b2s,b3s,b4s,b7,b8,b9);
}
}
}
fclose(fp);
fclose(fp2);
//NOW THE TMP FILE WILL BE RENAMED IN ORDER TO TAKE THE PLACE OF THE
ORIGINAL FILE
rename("insert.txt","materials3.txt");
rename("materials.txt","materialsi.tmp");
rename("materials3.txt","materials.txt");
if(count==0)
{
printf("\nValue not Found\n");
}
}