473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 OrderFileCreator
{
private string fileName = "";
private FileStream fs;
private StreamWriter sw;

{
this.fileName = fileName;

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

public void writeLine(string val0, string val1, string val2, string val3,
string val4)
{
string linie =
"\""+val0+"\",\""+val1+"\",\""+val2+"\",\""+val3+" \",\""+val4+"\"\n";
Byte[] info = new UTF8Encoding(true).GetBytes(linie);
this.fs.Write(info, 0, info.Length);
this.sw.WriteLine(linie);
}
public void writeOrderHead(string val1, string val2, string val3, string
val4, string val5)
{
string linie =
"\""+val1+"\",\""+val2+"\",\""+val3+"\",\""+val4+" \",\""+val5+"\"\n";
Byte[] info = new UTF8Encoding(true).GetBytes(linie);
this.fs.Write(info, 0, info.Length);
this.sw.WriteLine(linie);
}

}
}
Best regards

Mads
Dec 6 '05 #1
3 15610
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*********@topscene.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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****************@TK2MSFTNGP11.phx.gbl...
.Close() is all that is needed.

--

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

<jo*********@topscene.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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
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...
2
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...
9
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...
6
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...
3
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...
12
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...
2
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...
4
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....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.