PC Review


Reply
Thread Tools Rate Thread

C# Newbie needs help with simple code :(

 
 
Tony
Guest
Posts: n/a
 
      25th Sep 2005
{//start Button4
FileStream myFStream = new FileStream (@"C:\Documents
and Settings\Tony\Desktop\testfile.txt",
FileMode.Append, FileAccess.Write);
//FileMode.OpenOrCreate, FileAccess.ReadWrite);
TextWriter txtWrit = new TextWriter (myFStream);
//BinaryWriter binWrit = new BinaryWriter(myFStream);
string testString = "This is a test string.";
txtWrit.Write(testString);
//binWrit.Write(testString);
txtWrit.Close();
//binWrit.Close();
myFStream.Close();
}//End Button4


Looking at some examples I found, I was able to open a file and write
to it in binary mode using the commented out lines above..

Looked at the text in the file and it had a high-ascii char in it.

Thought the logical thing to do would be to use TextWriter instead of
BinaryWriter.

Running the code above though results in an error

C:\Documents and Settings\Tony\My Documents\Visual Studio
Projects\Justmessin\Form1.cs(188): Cannot create an instance of the
abstract class or interface 'System.IO.TextWriter'


I found info at
http://www.functionx.com/vcsharp/fil...g/Lesson04.htm
that says
"This class is abstract, meaning you can't initialize a variable with
it. Instead, you can use one of its derived classes, such as the
StreamWriter class."

But I don't understand it really... and why would binaryWriter be ok
to use, but not textWriter...


-Tony!-



 
Reply With Quote
 
 
 
 
Chris Taylor
Guest
Posts: n/a
 
      25th Sep 2005
Hi,

The TextWriter is an abstract class and therefore you can not create the
instance. What you are looking for is a class that derives
from the TextWriter class. You will want to look at the StreamWriter for
writing to streams. You could also use the File.CreateText which will return
a concrete TextWriter i.e.. an instance of a class deriving from TextWriter.

Additionally to what you have I would recommend that you look at using
either a try/finally block to ensure that the streams you open are closed or
better yet use the using statement. For reference see the following link
which also contains an example of using the File.CreateText
http://msdn.microsoft.com/library/de...pspec_8_13.asp

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor


"Tony" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> {//start Button4
> FileStream myFStream = new FileStream (@"C:\Documents
> and Settings\Tony\Desktop\testfile.txt",
> FileMode.Append, FileAccess.Write);
> //FileMode.OpenOrCreate, FileAccess.ReadWrite);
> TextWriter txtWrit = new TextWriter (myFStream);
> //BinaryWriter binWrit = new BinaryWriter(myFStream);
> string testString = "This is a test string.";
> txtWrit.Write(testString);
> //binWrit.Write(testString);
> txtWrit.Close();
> //binWrit.Close();
> myFStream.Close();
> }//End Button4
>
>
> Looking at some examples I found, I was able to open a file and write
> to it in binary mode using the commented out lines above..
>
> Looked at the text in the file and it had a high-ascii char in it.
>
> Thought the logical thing to do would be to use TextWriter instead of
> BinaryWriter.
>
> Running the code above though results in an error
>
> C:\Documents and Settings\Tony\My Documents\Visual Studio
> Projects\Justmessin\Form1.cs(188): Cannot create an instance of the
> abstract class or interface 'System.IO.TextWriter'
>
>
> I found info at
> http://www.functionx.com/vcsharp/fil...g/Lesson04.htm
> that says
> "This class is abstract, meaning you can't initialize a variable with
> it. Instead, you can use one of its derived classes, such as the
> StreamWriter class."
>
> But I don't understand it really... and why would binaryWriter be ok
> to use, but not textWriter...
>
>
> -Tony!-
>
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linq to XML--Are there code examples that make Linq as easy as SQL? Or how can I convert ths simple pseudo code into real code? Reece Microsoft C# .NET 4 10th Dec 2008 03:13 AM
Newbie questions - probably simple Sonnich Microsoft Excel Programming 4 1st May 2006 08:42 PM
newbie.. simple question.. marco_pb via AccessMonster.com Microsoft Access VBA Modules 4 14th Mar 2006 07:13 AM
Simple question from simple newbie Vinito Microsoft VB .NET 5 3rd Aug 2004 11:05 PM
Newbie: simple text file code won't work- plz help! K Ruck Microsoft Dot NET Compact Framework 0 3rd Nov 2003 02:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:44 AM.