473,718 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using streamwriter to write CSV, but empty files

Hi,

I'm making some changes to my program, because I need to write with Codepage
850.
Before I used FileStream, but I found code to use with StreamWriter instead.

My problem is, that I don't get any output when writing with StreamWriter,
but only with FileStream.

I got StreamWriter to work in another class, where I only have 1 Writeline
method.
In my code, I'm trying to debug using both FileStream and StreamWriter to
write the file. I have tried to outcomment the fs.write, but without luck.

My files are created fine, but the're just empty!

What am I doing wrong? Souce code here:

using System;
using System.Text;
using System.IO;

namespace SAEXML2CSV
{

public class OrderFileCreato r
{
private string fileName = "";
private FileStream fs;
private StreamWriter sw;

{
this.fileName = fileName;

//opret ny fil
if(File.Exists( this.fileName))
Console.WriteLi ne("\nFilen fandtes allerede, den gamle fil er nu
overskrevet");
File.Delete(thi s.fileName);
if(!File.Exists (this.fileName) )
{
fs = File.Create(thi s.fileName);
sw = new StreamWriter(fs , Encoding.GetEnc oding( 850 ));
}
}

public void writeLine(strin g val0, string val1, string val2, string val3,
string val4)
{
string linie =
"\""+val0+"\",\ ""+val1+"\",\"" +val2+"\",\""+v al3+"\",\""+val 4+"\"\n";
Byte[] info = new UTF8Encoding(tr ue).GetBytes(li nie);
this.fs.Write(i nfo, 0, info.Length);
this.sw.WriteLi ne(linie);
}
public void writeOrderHead( string val1, string val2, string val3, string
val4, string val5)
{
string linie =
"\""+val1+"\",\ ""+val2+"\",\"" +val3+"\",\""+v al4+"\",\""+val 5+"\"\n";
Byte[] info = new UTF8Encoding(tr ue).GetBytes(li nie);
this.fs.Write(i nfo, 0, info.Length);
this.sw.WriteLi ne(linie);
}

}
}
Best regards

Mads
Dec 6 '05 #1
3 15722
Make sure you flush and then close the StreamWriter...

sw.Flush();
sw.Close();

JP

Dec 6 '05 #2
..Close() is all that is needed.

--

Derek Davis
dd******@gmail. com

<jo*********@to pscene.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Make sure you flush and then close the StreamWriter...

sw.Flush();
sw.Close();

JP

Dec 7 '05 #3
Thanks alot,
Works flawlessly!

Thumbs up from here :)

Best regards
Mads

"carion1" <dd******@gmail .com> skrev i en meddelelse
news:OU******** ********@TK2MSF TNGP11.phx.gbl. ..
.Close() is all that is needed.

--

Derek Davis
dd******@gmail. com

<jo*********@to pscene.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Make sure you flush and then close the StreamWriter...

sw.Flush();
sw.Close();

JP


Dec 7 '05 #4

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

Similar topics

1
1525
by: Daniel | last post by:
Is there any way for System.IO.StreamWriter Write method to write out part of the string to file. such as if the machine is shut down half way through? or does the file not actually exist until the entire write is completed successfully.
2
1624
by: Angel | last post by:
How much harder is it to write data to an XML file instead of an ascii file? When I write an output data file, I usually write it as an ascii file. But the client's been thinking of writing it in XML so the files can be used in other processes (eg. displaying data in web). Is writing it in XML format more time-consuming (and harder) than writing it in a plain ascii file? I usually read/write ascii files with StreamReader and StreamWriter...
9
4647
by: John | last post by:
Hi, I need to write out bits that I receive from another process. These are boolean values. I need there to be 8 bits in every byte. I know I could write these bits out as char's using one bit per byte, but that would be space-inefficient. I'm using od (octal dump) to look at a file produced by calling ..Write(true) and .Write(false) and it looks like it writes out 4 bytes per boolean value.
6
3981
by: kids_pro | last post by:
Hi there, using(StreamWriter wr = new StreamWriter(filename,false,Encoding.UTF8,1024)){} Will the line above throw any exception when it can't create file? What is the best way to capture it exceptions? Cheers, Kids
3
1613
by: Daniel | last post by:
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another process.
12
11117
by: Nina | last post by:
Hi there, What is the maximum length that one line can hold in a text file using StreamWriter's Write or WriteLine method? If the string is too long for one line to hold, what will Write or WriteLine do? In order me to retrieve data correctly from the text file later, I have to know the right index. For the following code,if str1's length exceeds one line in text file, how can I get str2's index when reading the text file? Is there...
2
3363
by: sameer | last post by:
Hi, My very simple application VB.NET Visual Studio 2003 console application uses a log file to write some text to it. I am using StreamWriter object to create and open and then append text to the log file. Now what happens is that if i have the folder in which the stream object creates the log file open in windows explorer, i can see that it creates the log file but then it crashes, strnage , can somebody tell me how i can stop this...
4
2303
randome
by: randome | last post by:
Hi. I've written a sample application to test my service. The problem is that I cannot write to files. I've tried setting up serviceProcessInstaller1.Account as LocalSystem, LocalService and User. Installation is successful with InstallUtil and service starts without a problem, creates a file C:\mylog.txt, reports no errors but it doesn't write in the file itself. Here is my code protected override void OnStart(string args) ...
0
8724
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
9356
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
9207
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...
1
9121
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9053
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
4481
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
4743
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3182
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2555
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.