By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,990 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,990 IT Pros & Developers. It's quick & easy.

StreamWriter, need to appendtext and not overwrite af file

P: n/a
Hi,

I'm parsing some XML files, and converting them to CSV.
I got x number of XML files in a directory, these files do I need to be
converted and written to the same CSV file.
My problem is that my csv file only contains the data from the last XML file
parsed...

I got the following code for creating of my StreamWriter:
public OrderFileCreator(string fileName)
{
this.fileName = fileName;
if(File.Exists(this.fileName))
{
fl = File.OpenWrite(this.fileName);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
if(!File.Exists(this.fileName))
{
fl = File.Create(this.fileName);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
}
Best regards
Mads
Mar 31 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi, Vadym,

I finally googled my way to another solution.
I used the following:
if(File.Exists(this.fileName))
{
fl = new FileStream(this.fileName, FileMode.Append);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
if(!File.Exists(this.fileName))
{
fl = File.Create(this.fileName);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
That worked fine.

I'm sure your solution would have worked to.
In my case, I don't care what end of the file i write to. But if that was a
issue, then your solution would come in handy.

Thaks!

Best regards
Mads

"Vadym Stetsyak" <va*****@ukr.net> skrev i en meddelelse
news:ug**************@TK2MSFTNGP11.phx.gbl...
Hello, Mads!

MW> I'm parsing some XML files, and converting them to CSV.
MW> I got x number of XML files in a directory, these files do I need to
be
MW> converted and written to the same CSV file.
MW> My problem is that my csv file only contains the data from the last
XML
MW> file parsed...

MW> I got the following code for creating of my StreamWriter:
MW> public OrderFileCreator(string fileName)
MW> {
MW> this.fileName = fileName;
MW> if(File.Exists(this.fileName))
MW> {
MW> fl = File.OpenWrite(this.fileName);
MW> fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
MW> }
MW> if(!File.Exists(this.fileName))
MW> {
MW> fl = File.Create(this.fileName);
MW> fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
MW> }
MW> }

The problems is the you start writing from the beginning of the file. You
need to "rewind" stream to the end.
You can do this with FileStream. fl.Position = fl.Length; or with
fl.Seek(...)

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Mar 31 '06 #2

P: n/a


Mads Westen wrote:
if(File.Exists(this.fileName))
{
fl = new FileStream(this.fileName, FileMode.Append);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
if(!File.Exists(this.fileName))
{
fl = File.Create(this.fileName);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}


What's wrong with simply:

Stream s = File.Open(fileName, FileMode.Append);
StreamWriter sw = new StreamWriter(s, enc);

BTW, you should probably be "using" s and sw, or have some other method
of ensuring that they get closed.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Mar 31 '06 #3

P: n/a
HJ> What's wrong with simply:

HJ> Stream s = File.Open(fileName, FileMode.Append);
HJ> StreamWriter sw = new StreamWriter(s, enc);

If you plan only to write to thele this is okay, because
File.Open(fileName, FileMode.Append) - opens file with FileAccess.Write.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 31 '06 #4

P: n/a


Vadym Stetsyak wrote:
HJ> What's wrong with simply:

HJ> Stream s = File.Open(fileName, FileMode.Append);
HJ> StreamWriter sw = new StreamWriter(s, enc);

If you plan only to write to thele this is okay, because
File.Open(fileName, FileMode.Append) - opens file with FileAccess.Write.


I think you can assume that only writes are to be done, since the
original example did new FileStream(name, FileMode.Append) -- which i
would believe is equivalent.

I just found it odd to check that the file exists first. It complicates
the code and introduces a race condition, probably not a bad one, but
still...

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Mar 31 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.