By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Replacing line in a file

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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);

"Chad" <ch**@mizmoz.com> wrote in message
news:bb**************************@posting.google.c om...
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.

Jul 19 '05 #2

P: n/a
Chad wrote:
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.

Jul 19 '05 #3

P: n/a
Joe Estock wrote:
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

Jul 19 '05 #4

P: n/a

"Joe Estock" <je*****@nutextonline.com> wrote in message
news:vr************@corp.supernews.com...
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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.