Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 11th, 2004, 11:44 AM
Authorized User
 
Join Date: Sep 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Write ASCII text file

I have try something on System.IO class to create a file in ASCII format. Unfortunately, I can't get it done. Something wrong with my code. Could anyone give me some advices?

Code:
protected LogWriter logWriter;

bool dirExists = validateDir();  // Create directory if not exist

if (dirExists) {

   bool fileExists = validateFile();  // Create file if not found

   if (fileExists) {
      StreamWriter sw = new StreamWriter(file, true, System.Text.Encoding.ASCII);

      logWriter = new LogWriter(sw);
      logWriter.log("Test1");
   }
}


// Class LogWriter
public class LogWriter {
   private TextWriter tw;

   public LogWriter(TextWriter tw) {
      this.tw = tw;
   }

   public void log (string message) {
      tw.WriteLine("[{0} : {1}] =>", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
      tw.WriteLine("{0}", message);
      tw.WriteLine("\n");
      tw.Flush();
      tw.Close();
   }
}

// End Class LogWriter
The output :
[Saturday, December 11, 2004 : 11:42:58 PM] =>
Test 1(square box)


Remark : (square box) is represent a square box when I use notepad to open the output file.


Thanks.


 
Old December 12th, 2004, 09:40 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your code looks fine. Are you saying that the file format is incorrect?

Kep.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Write Tab-delimited text file VikramMullick Pro VB.NET 2002/2003 1 May 5th, 2006 11:18 AM
How to write to text file ? method Beginning VB 6 1 April 13th, 2006 01:54 AM
Reading ASCII data from text file. LordBeholder VB How-To 2 June 25th, 2004 05:50 PM
Write Text file from recordset rylemer Access 2 December 5th, 2003 10:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.