473,382 Members | 1,658 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,382 software developers and data experts.

Modify text files by set back file pointer

Hello!

I supposed to modify some text files. In the file(s) which consist of
different section. In one section you might have
Path = <Installation_path>. The program will then replace the text string
Installation_path with the path that was chosen in the installation
program. An example could be this string <Installation_pathwould be
replaced with this path string C:\Work\PK\Program\Test\

Now to my question is it possible to set the file pointer back when using
text files in some way.
If it's not possible to set back the file pointer I have to use temporary
files instead.

//Tony
Nov 22 '07 #1
3 1920
On Nov 22, 8:42 am, "TonyJ" <johansson.anders...@telia.comwrote:
I supposed to modify some text files. In the file(s) which consist of
different section. In one section you might have
Path = <Installation_path>. The program will then replace the text string
Installation_path with the path that was chosen in the installation
program. An example could be this string <Installation_pathwould be
replaced with this path string C:\Work\PK\Program\Test\

Now to my question is it possible to set the file pointer back when using
text files in some way.
If it's not possible to set back the file pointer I have to use temporary
files instead.
Well, you can change where a StreamReader is, although it can be
tricky in some cases. However, you can't just insert or delete data
into/from a file - in your case c:\Work\PK\Program\Test\ is longer
than <Installation_pathso while you could overwrite
<Installation_pathyou would then overwrite the following text as
well.

I suggest you read the whole file into memory, then write out the
changed version.

Jon
Nov 22 '07 #2
Hi,

how do you open the file and what class do you use?
Do you split/tokenize the file content? Do you
place the results into a tree/list? How do you do this?

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
ke*****@arcor.de

Best Quote: "Ain't nobody a badass with a double dose
of rock salt...", Kill Bill Vol.2

Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
Sign my guestbook: http://entwicklung.junetz.de/guestbook/
-----------------------
"This reply is provided as is, without warranty express or implied."
Nov 22 '07 #3
On 2007-11-22 00:45:47 -0800, "Jon Skeet [C# MVP]" <sk***@pobox.comsaid:
On Nov 22, 8:42 am, "TonyJ" <johansson.anders...@telia.comwrote:
>I supposed to modify some text files. In the file(s) which consist of
different section. In one section you might have
Path = <Installation_path>. The program will then replace the text string
Installation_path with the path that was chosen in the installation
program. [...]

Well, you can change where a StreamReader is, although it can be
tricky in some cases. However, you can't just insert or delete data
into/from a file - in your case c:\Work\PK\Program\Test\ is longer
than <Installation_pathso while you could overwrite
<Installation_pathyou would then overwrite the following text as
well.

I suggest you read the whole file into memory, then write out the
changed version.
Agreed. Or alternatively, read the file in piece by piece, replacing
text and writing it out as you go along. Even changing it in memory is
going to have problems similar to changing it in the file (though of
course rearranging the data in memory to accomodate changes in the
length of replaced text is easier, it's still extra work somewhere).

Assuming the file has line breaks and that a token to be replaced never
spans two or more lines, then a single line would be a convenient unit
to read at a time. You could use StreamReader.ReadLine(), then
String.Replace() to replace tokens, then StreamWriter.WriteLine() to
write out the new file.

You could do all the same reading the whole file into memory (using
instead ReadToEnd()), but then you'd be limited in the size of the
file, and performance would degrade as the file gets larger compared to
a line-by-line approach.

Granted, it may well be that the intended text files are so small these
issues don't matter, but in this case it's practically no difference to
implement a line-by-line version compared to reading everything at once
(an loop and then deleting the original file and moving the final file
back to the original file name is pretty much the only added code), I'd
go ahead and do that.

YMMV. :)

Pete

Nov 22 '07 #4

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

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
4
by: pmud | last post by:
Hi I have a website (ASP.NET project using C# ) which is already put up on the server. I need to make some modification to some web pages.So the project files were copied to the a different server...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
4
by: c++ newbie | last post by:
hello gurus of C++ programming, I am a student doing my masters in computer applications.I am a bit experienced in VB 6.0.I am currently doing a project in vb 6. To accompish a certain task I...
2
by: =?Utf-8?B?YW5rMmdv?= | last post by:
Thanks in advance for reading this. Let's say I have a file (file01) with this data in ASCII (ignore line col): line01 123abc line02 Header01 Starts blah var line03 detail01 000001...
4
cassbiz
by: cassbiz | last post by:
Could use some help here. This script is carrying over an image just fine but the text isn't coming over. can you see why it is not working???? from the form I want to carry over two lines of...
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.