StreamWriter problem

  • Thread starter Thread starter Vannela
  • Start date Start date
V

Vannela

I want to open a new file and keep on appending the text
depending on some condition.

I have used this code but it is not appeneding properly

It is giving some numbers in the output file
if(index=="3")
{
files=new FileStream
(filetext1.Text+"\\"+tgname+"\\"+"GlobalDeclarations"+".cs"
,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);

swriter=new StreamWriter(files);

if(san==0)
{

swriter.WriteLine("using System;");

swriter.WriteLine("using System.IO;");

swriter.WriteLine("namespace "+ " "+tgname+"\r\n"+"{");
swriter.WriteLine
("public class GlobalDeclarations"+"\r\n"+"{");
}

}
swriter.WriteLine(rtb2.Text);


swriter.Close();
}


Where am i going wrong?

Please say me

Thank you.
 
Vannela said:
I want to open a new file and keep on appending the text
depending on some condition.

I have used this code but it is not appeneding properly

It is giving some numbers in the output file

What do you mean by "it is giving some numbers in the output file"?
Please give a short but complete program which demonstrates the
problem, describing exactly what you expected to happen and what did
happen.

See http://www.pobox.com/~skeet/csharp/complete.html for more of what I
mean by this.
 
What actually i want is when the index is some value (for
example 2 or 5)which i want then the text in the rich text
box of my form should get append to the file, if the file
exists or create a new file if it does not exists ?

And once the file is created if the condition is true ie.,
if the index value is 2 or 5 then the text in the rich
text box should get appended to the file created.

My code is as follows

if(index=="2" || index=="5")
{

files=new FileStream
(filetext1.Text+"\\"+tgname+"\\"+"GlobalDeclarations"+".cs"
,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);

swriter=new StreamWriter(files);
swriter.WriteLine(rtb2.Text);
swriter.Close();
}


But the output i am getting is somthing like this

000000000 75 73 69 6E 67 20 53 79 73 74 65 6D 3B 0D 0A
75using
000000010 74 69 24 24 44 44 3D 43 48 4F 0D 3B 0D 2E 6D
System
............................................................
....................................................etc
in this way

What could be the problem
 
Vannela said:
What actually i want is when the index is some value (for
example 2 or 5)which i want then the text in the rich text
box of my form should get append to the file, if the file
exists or create a new file if it does not exists ?

And once the file is created if the condition is true ie.,
if the index value is 2 or 5 then the text in the rich
text box should get appended to the file created.

My code is as follows

if(index=="2" || index=="5")
{

files=new FileStream
(filetext1.Text+"\\"+tgname+"\\"+"GlobalDeclarations"+".cs"
,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);

swriter=new StreamWriter(files);
swriter.WriteLine(rtb2.Text);
swriter.Close();
}

No, that's *part* of your code. Please post *complete* code. Did you
read the page I posted a link to?
But the output i am getting is somthing like this

000000000 75 73 69 6E 67 20 53 79 73 74 65 6D 3B 0D 0A
75using
000000010 74 69 24 24 44 44 3D 43 48 4F 0D 3B 0D 2E 6D
System
...........................................................
...................................................etc
in this way

What could be the problem

When you say "the output you are getting is", do you mean that's the
whole output of the file? Was there a file there before? What were you
expecting to get differently?

Please post enough information so that I (or someone else) can fully
reproduce this, with exactly what you expected to happen and what
happened differently. Currently you're leaving us guessing.
 
I could not read the site you specified b'coz the access
is denied for that site in our college.

When i say output i mean the text in the file ie., the
whole output of the file.

The file is being created when the condition satisfies for
the first time , and from the second time onwards the text
in the richtextbox of form should get appended to the file
created.

But i thing the problem is somewhere in the creation of
the file because if the condition is satisfying only for
one time then the output of my file is correct ie., i am
getting the output what i am expecting. But if the
condition is satisfying for more than one time then i am
not getting the correct output.

rtb2.Text is the text in the richtextbox which i want to
have in the file GlobalDeclarations.cs

filetext1.Text="d:\santhu\projects\current\PB8.0";
tgname="cd_core";

if(index=="2" || index=="5")
{

files=new FileStream
(filetext1.Text+"\\"+tgname+"\\"+"GlobalDeclarations"+".cs"
,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);

swriter=new StreamWriter(files);
if(san==0)
{
swriter.WriteLine("using System;");
swriter.WriteLine("using System.IO;");
swriter.WriteLine("namespace "+ " "+tgname+"\r\n"+"{");

swriter.WriteLine("public class
GlobalDeclarations "+"\r\n"+"{");
}

swriter.WriteLine(rtb2.Text);
swriter.Close();
}
 
Back
Top