473,725 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Edit lines in a txt file.

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,*vr ef,*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(fi lename,"r"))==N ULL)
{
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,"ma terial_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(inp utString, 2048, fp)!=NULL)
{
i++;
//SKIPS THE HEADER
if(i>1)
{//MAKES THE ATRIBUTION OF EACH VALUE TO IT'S RESPECTIVE VARIABLE
tempString=strt ok(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=tempStrin g;
tempString = strtok(NULL, seps);
alfam=tempStrin g;
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,alfa s,alfam,bs,bm,b 5,b6,b1m,b2m,b3 m,b4m,b1s,b2s,b 3s,b4s,b7,b8,b9 );
count++;
do
{//SWITCHES THROUGH THE OPTIONS
printf("\n\tWic h 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",&o ption);
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",alfa s);
break;
case 5:
printf("Insert new value for alfam: ");
scanf("%s",alfa m);
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,alfa s,alfam,bs,bm,b 5,b6,b1m,b2m,b3 m,b4m,b1s,b2s,b 3s,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,v ref,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,v ref,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","materials 3.txt");
rename("materia ls.txt","materi alsi.tmp");
rename("materia ls3.txt","mater ials.txt");

if(count==0)
{
printf("\nValue not Found\n");
}

}

Sep 14 '06 #1
3 3064
zb****@gmail.co m wrote:
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.

Sep 14 '06 #2
zb****@gmail.co m writes:
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,*vr ef,*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(fi lename,"r"))==N ULL)
{
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,"ma terial_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(inp utString, 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=strt ok(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=tempStrin g;
tempString = strtok(NULL, seps);
alfam=tempStrin g;
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.

--
Andrew Poelstra <http://www.wpsoftware. net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Sep 14 '06 #3
"Nils O. Selåsdal" wrote:
>
zb****@gmail.co m wrote:
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.
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.hfuncti ons,
and then to rewrite the whole file from the list.

--
pete
Sep 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4272
by: matt | last post by:
I am using a text file as a database, each field delimited by || I want to be able to print all lines to a page, then by selecting one (with, say, a radio button) it will load into the form at the bottom of the page, where I can edit the fields, and save back to the same line in the text file. I dont know how to use primary keys or anything with a text file database, any help in this would be appreciated! Thanks matt
1
8378
by: Tom | last post by:
Hello I am a newbie to VB .net 2003 environment. I have a database in sql server 2000 that was developed using vb. I would like to edit some of the .exe files that are used to collect and manipulate the data. I have vb .net 2003 but when I open the .exe file it shows me the binary info and not the windows that was developed. The window developed is a simple window that you would inut data into. How can I edit this .exe file in a gui...
2
4882
by: programming | last post by:
Hey all i am trying to write data that i have stored in a session back to a member.txt, but i am unable to work out why it wont write. It displays the data i have stored in the session ok, but it wont do the next step! I have used a similiar script in another part of my web page which works, but this one won't. From my understanding it has something to do with if statement if($data == $user_id), so i might be trying to compare the...
1
2418
by: BiT | last post by:
Hello I Know how to create and add elements to xml file with XmlTextWriter, here is code for example: Dim textWriter As XmlTextWriter = New XmlTextWriter("table.xml", Nothing) ' Opens the document textWriter.WriteStartDocument()
4
2401
by: Sanchit | last post by:
I want to know thta how can i edit a file in C++ For Example my file is Mr XyZ FFFFFF 65 And now i want go change this number 65 to 87.... how can i Do this..... I dont want to make a new file but want to edit this file only.
7
11091
dmjpro
by: dmjpro | last post by:
Hi All .... I am trying to edit a file using RandomAccessFile. I am having a little bit problem. My file is ... i m debasis jana.
1
1741
by: irfan123 | last post by:
respected forum members plz help me edit bin file. which tools to be used, for editing bin file.
1
1274
by: inciguleray | last post by:
I want to knw that how to edit a file? how to delete a whole file? how to add some text at a specified location in a file ? how to delete something from a specified location in a file?
1
1921
by: Anke Weiland | last post by:
Hello, I have to insert the GATC (Google Analytics Tracking Code) on every single page of our website. So, I got the code, and I know where I have to insert it, just before </head>. So I go to the homepage, right-klick and view 'page source'. This gets me to an .aspx file by using internet explorer or to a default.aspx file by using firefox. As I never had to change such a file before, I do not know how I can edit this file and be...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.