Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 26th, 2006, 12:16 PM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile Changing a line in a text file

Let's say I have a text file like this:
one
two
three
four
five
...

and I want to change the line containing 'four' to 'vier'. Right now I read it all into a list, process it and write it all back out. Is there a way to do this on the fly with the existing file?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Aug 26th, 2006, 1:25 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Yes and no. You can alter individual bytes of a file. So in the example you provide, where "four" is the same length as the string to replace it, "vier", one could just alter those four bytes. If the size of the word differs, however, one would have to rewrite all bytes in the file that come after it.

For example, if I wished to replace "out" with "in":
hello out there
hello in there
I'd have to move "there" back one character, as the file size has decreased by one. This would involve rewriting the latter half of the file (highlighted in red).

Thus, for most search and replace operations, there's no real disadvantage to reading the entire file into RAM and altering it there (unless you're likely to run out of RAM).
Arevos is offline   Reply With Quote
Old Aug 26th, 2006, 2:35 PM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Okay, I can see the problem.
Thanks Arevos
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 8:55 AM
Help in QBASIC (I think it's similar to VB) phoenix987 Visual Basic 3 May 9th, 2005 12:33 PM
Help with a QBASIC program phoenix987 Other Programming Languages 4 May 5th, 2005 12:27 PM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC