Edit lines in a txt file.

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");
}

}
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

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.
Remember that you cannot insert into files. You can only overwrite or
append content. Thus if you want to change a line and make it larger,
you need to preserve what's following that line, else you'll overwrite
that.
Search this newsgroup for alternative suggestions.
 
A

Andrew Poelstra

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)

You'd best return an int; you appear to be opening files, and you need
some way to return the caller an error code.
{
FILE *fp;
FILE *fp2;

How about "ifp" and "ofp" for input and output file pointers?
int count=0;
char inputString[MAXSTRING];
int i=0;
int option;
char seps[]=" ";

If youre not going to change that, then using a pointer instead of an
array might be clearer.
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;

Oh, my. Use an array for some or all of these, especially the ones with
numbers in them.
char outputString[MAXSTRING];
//CHECKS IF THE ORIGINAL FILE CAN BE OPENED

Using /* ... */ comments is more portable, and IMHO, better style. Also,
using all caps is still shouting, even in comments. It's hard to read.
Finally, you're explaining idiomatic C; the comment itself is not very
necessary.
if((fp=fopen(filename,"r"))==NULL)
{
printf("Error opening the file %s",fp);
}

Don't use tabs on Usenet; they'll either fail to display, or they'll
almost certainly overrun my 80-character screen at some point.
//CHECKS IF THE TMP FILE CAN BE OPENED

If it's a temp you could have named it tmpfp instead of fp2.
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 spy with my little eye a magic number. You used MAXSTRING above, so
why not here?
{
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)

This is what I mean. I can't read anything from here to my above
comment, because your tabs have literally split each line halfway
across my screen. Also, if you're 8 tablevels deep, you need to
seriously rethink your code. 3 levels is a general limit, although
even 5 is forgivable in a lot of situations. (In your case, you
may just have to rethink your tabbing style; a lot of those indents
were unnecessary.)

Finally, it's poor netiquette to post lines that are over 72 chars
long. Since I've been unable to read a substantial portion of your
code, I'll stop here.
 
P

pete

Nils O. Selåsdal said:
Remember that you cannot insert into files. You can only overwrite or
append content. Thus if you want to change a line and make it larger,
you need to preserve what's following that line, else you'll overwrite
that.
Search this newsgroup for alternative suggestions.

Maybe overkill but, a simple way,
is to load the whole text file into a linked list,
converting each line into a string for each list node,
and then to alter the strings,
perhaps using the <string.h> functions,
and then to rewrite the whole file from the list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top