Click here to Skip to main content
15,919,500 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
QuestionStreamReader, StreamWriter and File Position? Pin
BuckBrown15-Oct-07 9:18
BuckBrown15-Oct-07 9:18 
QuestionRe: StreamReader, StreamWriter and File Position? Pin
Mark Salsbery15-Oct-07 9:44
Mark Salsbery15-Oct-07 9:44 
AnswerRe: StreamReader, StreamWriter and File Position? Pin
BuckBrown15-Oct-07 10:17
BuckBrown15-Oct-07 10:17 
GeneralRe: StreamReader, StreamWriter and File Position? [modified] Pin
Mark Salsbery15-Oct-07 11:02
Mark Salsbery15-Oct-07 11:02 
GeneralRe: StreamReader, StreamWriter and File Position? Pin
BuckBrown16-Oct-07 9:05
BuckBrown16-Oct-07 9:05 
GeneralRe: StreamReader, StreamWriter and File Position? Pin
Mark Salsbery16-Oct-07 9:26
Mark Salsbery16-Oct-07 9:26 
GeneralRe: StreamReader, StreamWriter and File Position? Pin
BuckBrown16-Oct-07 10:11
BuckBrown16-Oct-07 10:11 
GeneralRe: StreamReader, StreamWriter and File Position? Pin
Mark Salsbery16-Oct-07 10:29
Mark Salsbery16-Oct-07 10:29 
BuckBrown wrote:
Is your 'if so...' pertaining to the first case or the second case?

Both, actually Smile | :)

After posting I thought about how you were getting those results
and figured you HAD to be using a common stream.

That's not going to work, at least (especially) not in a read and write loop
like you're doing.

There's two pointers each for both the reader and the writer involved here.
One is the stream position, which you have control of.
The other is the reader and writer's internal buffer pointer, which you don't
have control of.

In your first example, first iteration of the loop...
You request to read a line.  For efficiency, the reader reads an
entire buffer full (or the entire file, whichever is smaller). 
In this case, it looks like the entire file is buffered so the stream pointer
now points to the end of file.  All reads come from the readers buffer, all
writes now go to the end of the file, since that's where the stream pointer
was at the point of the first write.  I hope that makes sense.

Now let's say you manage to control all of this (using unbuffered I/O and random
access (seeking) to the stream).  If you read a string, add a 'X', write it back
to the file, and then try to read the next string, your 'X' will have overwritten the
next string in the file, unless you shift the remaining bytes in the file "to the right"
to make room for the 'X'.

This is why reading and writing each iteration of the loop is not going to work.
You need to do all the reading in one loop, make appropriate changes, then write
the results back to the stream.  Sharing the stream like you're doing is fine - just set the
stream position back to the appropriate place before starting the write loop.

Of course, if there's data in the file beyond what you've read, that would all have to be
shifted to make room for anything you add.

Mark



Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

GeneralRe: StreamReader, StreamWriter and File Position? Pin
BuckBrown16-Oct-07 11:12
BuckBrown16-Oct-07 11:12 
GeneralRe: StreamReader, StreamWriter and File Position? Pin
Mark Salsbery16-Oct-07 11:27
Mark Salsbery16-Oct-07 11:27 
GeneralRe: StreamReader, StreamWriter and File Position? Pin
BuckBrown16-Oct-07 11:30
BuckBrown16-Oct-07 11:30 
GeneralRe: StreamReader, StreamWriter and File Position? Pin
Mark Salsbery16-Oct-07 11:51
Mark Salsbery16-Oct-07 11:51 
QuestionHow to get HWND from System::Windows::Forms::Panel^ panel [modified] Pin
Newbie0014-Oct-07 0:35
Newbie0014-Oct-07 0:35 
AnswerRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Mark Salsbery14-Oct-07 5:53
Mark Salsbery14-Oct-07 5:53 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Newbie0014-Oct-07 7:02
Newbie0014-Oct-07 7:02 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Mark Salsbery14-Oct-07 7:19
Mark Salsbery14-Oct-07 7:19 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Newbie0014-Oct-07 7:29
Newbie0014-Oct-07 7:29 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Mark Salsbery14-Oct-07 8:00
Mark Salsbery14-Oct-07 8:00 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Newbie0014-Oct-07 8:02
Newbie0014-Oct-07 8:02 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Mark Salsbery14-Oct-07 8:22
Mark Salsbery14-Oct-07 8:22 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Newbie0014-Oct-07 10:04
Newbie0014-Oct-07 10:04 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Mark Salsbery14-Oct-07 10:24
Mark Salsbery14-Oct-07 10:24 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Newbie0014-Oct-07 10:32
Newbie0014-Oct-07 10:32 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
Mark Salsbery14-Oct-07 11:02
Mark Salsbery14-Oct-07 11:02 
GeneralRe: How to get HWND from System::Windows::Forms::Panel^ panel Pin
led mike15-Oct-07 4:36
led mike15-Oct-07 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.