473,804 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamWriter, need to appendtext and not overwrite af file

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 OrderFileCreato r(string fileName)
{
this.fileName = fileName;
if(File.Exists( this.fileName))
{
fl = File.OpenWrite( this.fileName);
fs = new StreamWriter(fl , Encoding.GetEnc oding( 850 ));
}
if(!File.Exists (this.fileName) )
{
fl = File.Create(thi s.fileName);
fs = new StreamWriter(fl , Encoding.GetEnc oding( 850 ));
}
}
Best regards
Mads
Mar 31 '06 #1
4 14376
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.GetEnc oding( 850 ));
}
if(!File.Exists (this.fileName) )
{
fl = File.Create(thi s.fileName);
fs = new StreamWriter(fl , Encoding.GetEnc oding( 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.ne t> skrev i en meddelelse
news:ug******** ******@TK2MSFTN GP11.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 OrderFileCreato r(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.GetEnc oding( 850 ));
MW> }
MW> if(!File.Exists (this.fileName) )
MW> {
MW> fl = File.Create(thi s.fileName);
MW> fs = new StreamWriter(fl , Encoding.GetEnc oding( 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


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


What's wrong with simply:

Stream s = File.Open(fileN ame, 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
HJ> What's wrong with simply:

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

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

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


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

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

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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5035
by: José Joye | last post by:
I have 2 threads using the same StreamWriter (obtained from FileInfo.AppendText). This first thread will always use the WriteLine() method and the second thread will always use the Flush() method. At the present, I have protected the access to the StreamWriter using a Lock() section. Is this really particular case, is this necessary? What could be the implication if I remove the Lock() section?
9
4599
by: ShadowOfTheBeast | last post by:
Hi, I have got a major headache understanding streamReader and streamWriter relationship. I know how to use the streamreader and streamwriter independently. but how do you write out using the streamwriter, what you have read into a streamReader? and also can someone explain how they work in simple terms -- The Matrix Insurrection
4
20568
by: rex64 | last post by:
I am getting an error message and I have not been able to figure hot how to fix it. I have done some research with no answers yet. I found this code that may help? Not sure what to do with it. using (StreamWriter sw = new StreamWriter (fullAddress , false, Encoding.UTF7)) Error message::::::::::::::::::::::::::::::::::::::::::::::
2
2235
by: cagey cretin | last post by:
I have set up a catch/try deal to find an error, but there are none reported nor when I clean the build/rebuild. Simple filecalled text_file.txt. I cannot write to it, but I can write into it with notepad and read it with StreamReader. Can anyone show me the error of my ways? //AppendText() Method #using <mscorlib.dll> using namespace System;
2
2081
by: David Buchan | last post by:
I have written a program to write some text to a file. Currently, if the file already exists, the program simply overwrites it. What I'd like to do, is have the program ask me if I wish to overwrite the existing file, ask for a new name if the answer is no, or overwrite if the answer is yes. I have the line: Dim objWriter As StreamWriter = New StreamWriter(OutputFileName)
2
10384
by: mosscliffe | last post by:
I have a simple form with a multiline Textbox. I load the textbox with the contents of a text file. I make changes to the textbox, then I attempt to OVERWRITE the original file. I have tried opening with FilMode.Create, FileMode.Truncate but they bothh APPEND to the file.
1
1719
by: ukgrl_yr79 | last post by:
hi fairly new to vb.net . I have created a filesystem where a manager and co workers hve limted access. the manager can read members files. i have used streamreader to get the details from a text file on my c drive into relevent text boxes eg name, address by using arrays.
1
2822
by: Diego F. | last post by:
Hola. Tengo este trozo de código: If File.Exists(Ls_NombreFichero) = False Then sw = File.CreateText(Ls_NombreFichero) Else sw = File.AppendText(Ls_NombreFichero) End If El caso es que necesito cambiar la codificación, porque el Encoding por defecto es utf-8 y quiero probar otro. ¿Cómo lo hago? La propiedad encoding
3
20260
by: Diego F. | last post by:
Hi. I'm using that code: If File.Exists(Ls_NombreFichero) = False Then sw = File.CreateText(Ls_NombreFichero) Else sw = File.AppendText(Ls_NombreFichero) End If I need to change the encoding, as utf-8 is not the one I can use. How can I change it? Encoding property is read only and I don't know how to use the
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10089
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9171
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7634
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.