Home > Archive > C# > February 2005 > File IO
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Steven 2005-02-18, 3:58 pm |
| Hi All,
I have a project required to save real time data into a file. At a
specified time, such as middle of night, close the old file and start
to save the data to a new file.
I have implemented the requirement through C#/FileStream and
StreamWriter. There is a MINOR problem: When a new file generated, a
piece of old data which is already saved into the old file was written
into the new file.
My code for file changing is like the folows:
if( m_StreamWriter != null )
{
m_StreamWriter.Flush();
m_StreamWriter.Close();
m_StreamWriter = null;
}
if( m_FileStream != null )
{
m_FileStream.Flush();
m_FileStream.Close();
m_FileStream = null;
}
........
m_strFileName = strNewFileName;
m_FileStream = new FileStream(m_strFileName, FileMode.OpenOrCreate,
FileAccess.Write);
m_StreamWriter = new StreamWriter(m_FileStream);
I can not figure out what is wrong about it. If anybody has any idea,
please let me know.
Thanks in advantace!
BTW: The characteristics of my real time data is sparse(there is no
data activaties most of time) in the middle of the night. The repeated
data saved is the last piece of data in the previous day.
Steven
| |
| Keenan Newton 2005-02-20, 8:57 pm |
| Well looking at the cide everything seems to be in order. So I am
assuming there is a lot more going on here. My suggestin is to create
a log file of all data that is sent to the stream. This way you know
what is exactly being sent and can throw a time stamp on it too.
Disprove the simpliest thing first. My theory is what is ever writing
to the StreamWriter object is the actual problem. Just a hunch though
| |
| Steven 2005-02-21, 4:00 pm |
| "Keenan Newton" <kameleon_dj@yahoo.com> wrote in message news:<1108934362.513564.325390@g14g2000cwa.googlegroups.com>...
> Well looking at the cide everything seems to be in order. So I am
> assuming there is a lot more going on here. My suggestin is to create
> a log file of all data that is sent to the stream. This way you know
> what is exactly being sent and can throw a time stamp on it too.
> Disprove the simpliest thing first. My theory is what is ever writing
> to the StreamWriter object is the actual problem. Just a hunch though
Thanks Keenan,
As a matter of fact, there is no too much to do when I used "......".
Just remove the code for generating the new file name and open new
filestream and streamwriter.
My data is self time stamped and the event for file name change only
happens once everyday. The data appears on both files are the last
piece of the previous day. and that piece data has been generated a
couple of hours early, I guess, because the activities became sparse
in the late hour, the buffer is not filled until the middle of night.
To my feeling, it seems when the stream closed, the buffer is not
emptied and somehow that buffer was reused and then the contents were
put into new stream. I don't have any support for this and it is only
my wild guess from desperation.
Any idea to support or to disagree about this?
Thanks,
Steven
| |
| Keenan Newton 2005-02-21, 8:59 pm |
| Well if what you suggest is the problem. I beleive there will be many
many more people complaning baout this including myself. What i was
trying to point to is that you are handing the m_StreamWriter off to
something so it can write data as needed. I am assuming somewhere in
your code m_StreamWriter.Write is being called. I beleive it is that
process where you will find the data being passed to thr writer twice.
Again this is just a hunch, I just don't think your theory is possible,
otherwise a lot of people would be complaining. You did all the right
things you flushed the writer, closed thr writer, flushed the
filestream, closed the filestream. Adn then you created brand new
objects for them. I can't see anything wrong with that
|
|
|
|
|