473,396 Members | 2,013 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,396 software developers and data experts.

Opening a file for both read/write

In C I can open a file as readable, writable, or both. Then I can
arbitrarily do either operation without closing the file between operations
as long as I perform some kind of file position indicator repositioning when
I change between reading and writing. In C# I'd like something like a
StreamReaderWriter that I could use the same way, but there doesn't appear to
be such a thing. I don't want to have to repeatedly close, reopen, and
reseek since I in my application I would have to do this for each byte that I
write. Is it possible to open the same file with both a StreamReader and a
StreamWriter simultaneously?

Thanks!
Ray
Aug 21 '07 #1
2 1869
Ray,

Assuming that you were holding a reference to the underlying FileStream
(or, you used the BaseStream property on the StreamReader or StreamWriter
class), you could have a StreamReader/StreamWriter open at the same time.
You would have to call Flush on the StreamWriter, to flush any buffered data
to the underlying stream, and then call DiscardBufferedData on the
StreamReader, to discard any data that the reader buffered. Then, you could
seek on the file stream, and you should be able to use the
StreamReader/StreamWriter again.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ray Mitchell" <Ra*****************@MeanOldTeacher.comwrote in message
news:B8**********************************@microsof t.com...
In C I can open a file as readable, writable, or both. Then I can
arbitrarily do either operation without closing the file between
operations
as long as I perform some kind of file position indicator repositioning
when
I change between reading and writing. In C# I'd like something like a
StreamReaderWriter that I could use the same way, but there doesn't appear
to
be such a thing. I don't want to have to repeatedly close, reopen, and
reseek since I in my application I would have to do this for each byte
that I
write. Is it possible to open the same file with both a StreamReader and
a
StreamWriter simultaneously?

Thanks!
Ray

Aug 21 '07 #2
Hi,

"Ray Mitchell" <Ra*****************@MeanOldTeacher.comwrote in message
news:B8**********************************@microsof t.com...
In C I can open a file as readable, writable, or both. Then I can
arbitrarily do either operation without closing the file between
operations
as long as I perform some kind of file position indicator repositioning
when
I change between reading and writing. In C# I'd like something like a
StreamReaderWriter that I could use the same way, but there doesn't appear
to
be such a thing. I don't want to have to repeatedly close, reopen, and
reseek since I in my application I would have to do this for each byte
that I
write. Is it possible to open the same file with both a StreamReader and
a
StreamWriter simultaneously?
Use FileStream

Aug 21 '07 #3

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

Similar topics

7
by: Fred H | last post by:
Hi I've tried to find out if and how I can open directories in standard C++, the same way I can open files. To my great suprise, I haven't managed to find out how this is done! I'm using MS...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
4
by: Omko H | last post by:
I am trying to open %windir%\pfirewall.log. and I just can't get it to work (I am a absolute newbie to VB.net) the strange thing is that notepad is perfectly able to view the file. but my code...
4
by: Satya | last post by:
I am trying to display a PDF file (which I am being passed from a web service as a binary stream) in a browser, but I am being prompted to save the file instead. I don't want the user to be...
11
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly...
1
by: raghu | last post by:
i want to know the difference between 'r' mode and 'r+' mode 1.i = open('c:\python25\integer.txt','w')-------->for writiing i.write('hai')--------->written some content in text file i =...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.