473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

text file editing

Jon
I am trying to accomplish the following with a text file...

1 open the file
2 loop through it line by line
3 if need be, modify the current line
4 replace the old line in the file with the newly modified line
5 close the file

I know how to do 1,2,3 and 5, but I'm not having any luck writing the
modified line back to the file (preferably at the same position) and
removing the old line.

Can anyone shed some light on that portion of it? Thanks
Nov 21 '05 #1
2 1169

"Jon" <ro*******@mainstreams.com> wrote in message
news:b6**************************@ALLTEL.NET...
I am trying to accomplish the following with a text file...

1 open the file
2 loop through it line by line
3 if need be, modify the current line
4 replace the old line in the file with the newly modified line
5 close the file

I know how to do 1,2,3 and 5, but I'm not having any luck writing the
modified line back to the file (preferably at the same position) and
removing the old line.

Can anyone shed some light on that portion of it? Thanks

One approach that comes to mind is to create a second file and write the
lines to it, replacing each edited line with the modified data. Once through
all lines in the original file, the second file would contain all the lines
from the original, but with modified lines in place.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 21 '05 #2
"Jon" <ro*******@mainstreams.com> schrieb
I am trying to accomplish the following with a text file...

1 open the file
2 loop through it line by line
3 if need be, modify the current line
4 replace the old line in the file with the newly modified line 5
close the file

I know how to do 1,2,3 and 5, but I'm not having any luck writing
the modified line back to the file (preferably at the same position)
and removing the old line.

Can anyone shed some light on that portion of it? Thanks

You can not modify a line unless it's length didn't change.

If the length has changed, read the rest of the file after the line to be
modified, set the Position property of the stream, write the new line and
write the rest of the file.

If the length did not change, set the Position property and write the new
line.
Take into account that, depending on the encoding of the file, the resulting
number of bytes in the file might change although the number of chars did
not change.

Armin

Nov 21 '05 #3

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

Similar topics

1
by: | last post by:
I am new to VB6 and need some advice... I am developing software which aims to capture a text feed from a serial port consisting of news stories in ASCII format and then save this text for...
11
by: Ed Suominen | last post by:
I'm thinking of implementing a real-time collaborative text editor in Python using Twisted. An initial plan is to use a Twisted PB server daemon that accepts user:password:file connections from...
13
by: Luigi | last post by:
Imagine I have a small, static, personal site (I do, actually: http://kirpi.it/). And imagine that, just below the title of the page, I would like to have a comment, or a news line, or a birthday...
9
by: Frances | last post by:
at work we switched to UTF-16 encoding and now when I open html files in HomeSite the code is all messed up, and if I turn on "enable non-ANSI file encoding" code looks fine when I open files but I...
2
by: Rudy Ray Moore | last post by:
Hi guys, I just upgraded to "Visual Studio .net 2003 7.1 c++" from VS6. Some things I like (proper for loop variable scoping, for example), but some other things are troubling me. One...
2
by: Tilo Pätzold | last post by:
Hi Everybody (especially Microsoft), we build EMF files with rotated text for export to office (powerpoint, word). It is planned that the text can be edited in the office document. Without the...
3
by: vlad.levin | last post by:
I'd like to write a control that allows a user to expand a div which originally shows the first line or two of text. I don't know very much javascript so I was hoping someone here could point me in...
63
by: John Salerno | last post by:
I know there's a request for a good IDE at least once a week on the ng, but hopefully this question is a little different. I'm looking for suggestions for a good cross-platform text editor (which...
3
by: cyberco | last post by:
I must be overlooking something here... I'm trying to edit a line in a text file. I thought this was easy with fileinput, but all examples do not write the line back to the file but simply 'print'...
10
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.