469,576 Members | 1,662 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

c# read/write to a file.

Hello All,

I apologise for such a basic question, I am just getting to grips with
c#, I usually develop in java.

What I am trying to do is:

I have a file which stores look-up information, as part of the
functionality of my application, I allow the user to change the lookup
data. Whats the best method in c# for editing a file.

For example:
I am trying to insert 10 lines at say line 50 of my file,
or another eg is I am trying to edit line 25 of my file??

Apologies again for such a basic question, any help on this matter
would be gratefully recieved.

Lloyd
Nov 15 '05 #1
3 1631

"lltaylor" <ll**********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
Hello All,

I apologise for such a basic question, I am just getting to grips with
c#, I usually develop in java.

What I am trying to do is:

I have a file which stores look-up information, as part of the
functionality of my application, I allow the user to change the lookup
data. Whats the best method in c# for editing a file.

For example:
I am trying to insert 10 lines at say line 50 of my file,
or another eg is I am trying to edit line 25 of my file??

Apologies again for such a basic question, any help on this matter
would be gratefully recieved.

Lloyd


If multiple threads (users) needs to have write-access to some datastore,
wouldn't it be better to store that data in a database?

As far as I know it is not possible (in any system) to add lines in the
middle
of a textfile, especially when other threads are changing other parts of
that file.

Hans Kesting
Nov 15 '05 #2
lltaylor,

If the file uses a text format, then you will want to use a StreamReader
(with the proper encoding) to read the file. If each line has a
fixed-length layout, then you can easily skip ahead to any line by just
reading the appropriate number of characters. This would probably be faster
because it would perform a seek on the underlying stream. However, if the
size of the lines varies, then you will just want to call ReadLine the
appropriate number of times.

If you are only moving forward, then this should be fine. However, if
you need to move randomly in your file, then you will want to access the
BaseStream property on your reader and call Seek on it to move an
appropriate number of places. Then, you will have to call
DiscardBufferedData on the reader so that the reader knows that you have
reset the position in the underlying stream.

Thinking about it now, it is probably better off if you just use the
BaseStream exposed by the StreamReader, as it will unify the code for
navigation.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"lltaylor" <ll**********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
Hello All,

I apologise for such a basic question, I am just getting to grips with
c#, I usually develop in java.

What I am trying to do is:

I have a file which stores look-up information, as part of the
functionality of my application, I allow the user to change the lookup
data. Whats the best method in c# for editing a file.

For example:
I am trying to insert 10 lines at say line 50 of my file,
or another eg is I am trying to edit line 25 of my file??

Apologies again for such a basic question, any help on this matter
would be gratefully recieved.

Lloyd

Nov 15 '05 #3
lltaylor <ll**********@yahoo.com> wrote:
I apologise for such a basic question, I am just getting to grips with
c#, I usually develop in java.

What I am trying to do is:

I have a file which stores look-up information, as part of the
functionality of my application, I allow the user to change the lookup
data. Whats the best method in c# for editing a file.

For example:
I am trying to insert 10 lines at say line 50 of my file,
or another eg is I am trying to edit line 25 of my file??

Apologies again for such a basic question, any help on this matter
would be gratefully recieved.


You can't insert or remove anything in a file (in any language) without
reading the rest of the file and writing it out again, basically. You
could either do this by reading the whole file into memory at once and
writing it out again after making the changes (eg using an ArrayList
with each line being an element) or by reading from the file and
writing to a temporary new file, inserting the new stuff as you go, and
then renaming the file afterwards.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Jason Heyes | last post: by
1 post views Thread by Magix | last post: by
8 posts views Thread by a | last post: by
5 posts views Thread by Sumana | last post: by
3 posts views Thread by nicolasg | last post: by
2 posts views Thread by Kevin Ar18 | last post: by
9 posts views Thread by vineeth | last post: by
1 post views Thread by Sachin Garg | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.