Replacing line in a file

C

Chad

Hello, im struggleing,Im trying to replace a line in a file.

1|010000
1|010001
1|010002
0|010003 <-- replace this with 1|10003
0|010004
0|010005
.... all of the strings are the same length.


I had been doing it by creating a temp file but it seems a bit of a
waste of resources. the file is nearly 2mb.

Any help would be greatly appreciated, thanks.
 
J

Joe Estock

Perhaps the simple way would be to read the file in line by line and compare
it to the string you want to replace it with and if it matches, seek back to
the beginning of the line, write out the string, and if you have no more
occurences of it then close the file, otherwise repeate the same operation.

e.g.,

myfile.txt
0|0123456
0|0123457
0|0123458

file *fp = fopen("myfile.txt", "a+");
char buffer[512];
char replace[] = "myreplacement";

fread(&buffer, fp);

if(!strcmp(buffer, replace))
{
fseek((strlen(buffer) - strlen(buffer) * 2), SEEK_CUR);
fwrite(&replace, fp);
}
fclose(fp);
 
G

Gianni Mariani

Chad said:
Hello, im struggleing,Im trying to replace a line in a file.

1|010000
1|010001
1|010002
0|010003 <-- replace this with 1|10003
0|010004
0|010005
... all of the strings are the same length.


I had been doing it by creating a temp file but it seems a bit of a
waste of resources. the file is nearly 2mb.

Any help would be greatly appreciated, thanks.


If you're not changing the length of the string, you could theoretically
seek to the position in the file where the new line starts and
over-write the new line.
 
T

Thomas Matthews

Joe said:
Perhaps the simple way would be to read the file in line by line and compare
it to the string you want to replace it with and if it matches, seek back to
the beginning of the line, write out the string, and if you have no more
occurences of it then close the file, otherwise repeate the same operation.

e.g.,

myfile.txt
0|0123456
0|0123457
0|0123458

file *fp = fopen("myfile.txt", "a+");
char buffer[512];
char replace[] = "myreplacement";

fread(&buffer, fp);

if(!strcmp(buffer, replace))
{
fseek((strlen(buffer) - strlen(buffer) * 2), SEEK_CUR);
fwrite(&replace, fp);
}
fclose(fp);
1. Don't top-post. Fix your newsreader so that replies are appended to
the bottom of a post.

2. File content can only be modified if the length of the new text is
the same as the old text. If the new text is shorter, the designer
needs to come up with some rules as to what the padding should be,
sinces files don't shrink.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
C

Chris Theis

Joe Estock said:
Perhaps the simple way would be to read the file in line by line and compare
it to the string you want to replace it with and if it matches, seek back to
the beginning of the line, write out the string, and if you have no more
occurences of it then close the file, otherwise repeate the same operation.

e.g.,

myfile.txt
0|0123456
0|0123457
0|0123458

file *fp = fopen("myfile.txt", "a+");
char buffer[512];
char replace[] = "myreplacement";

fread(&buffer, fp);

if(!strcmp(buffer, replace))

Just some comments. Hmm somehow I don't quite get this line because you
compare what you read in with the string that will be used as the
replacement. Normally the text which is to be replaced should be different
from its replacement because if it is not, then why replace it?
{
fseek((strlen(buffer) - strlen(buffer) * 2), SEEK_CUR);

(strlen(buffer) - strlen(buffer) * 2) isn't that the same
s -strlen(buffer) ?
fwrite(&replace, fp);
}
fclose(fp);

Cheers
Chris
 
J

Joe Estock

I ma unclear as to what you mean about "top posting". I simply replyed to
the relevent message, and if I am top-posting then I certainly appologize.
If you would give me an example so that I can see exactly what it is that I
am doing I would be more than happy to stop :)

Thomas Matthews said:
Joe said:
Perhaps the simple way would be to read the file in line by line and compare
it to the string you want to replace it with and if it matches, seek back to
the beginning of the line, write out the string, and if you have no more
occurences of it then close the file, otherwise repeate the same operation.

e.g.,

myfile.txt
0|0123456
0|0123457
0|0123458

file *fp = fopen("myfile.txt", "a+");
char buffer[512];
char replace[] = "myreplacement";

fread(&buffer, fp);

if(!strcmp(buffer, replace))
{
fseek((strlen(buffer) - strlen(buffer) * 2), SEEK_CUR);
fwrite(&replace, fp);
}
fclose(fp);
1. Don't top-post. Fix your newsreader so that replies are appended to
the bottom of a post.

2. File content can only be modified if the length of the new text is
the same as the old text. If the new text is shorter, the designer
needs to come up with some rules as to what the padding should be,
sinces files don't shrink.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
C

Chris Theis

Joe Estock said:
I ma unclear as to what you mean about "top posting". I simply replyed to
the relevent message, and if I am top-posting then I certainly appologize.
If you would give me an example so that I can see exactly what it is that I
am doing I would be more than happy to stop :)
[SNIP]

Actually what you do here is "top-posting". If you answer then you should
give the answer at the bottom of the previous posting. This makes it easier
for others to follow because they first read what question/topic you answer
to.

Cheers
Chris
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top