By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Writing to file in ASCII format + one more question

P: n/a
Hi,

1. I have a string that i want to write into a file. the string is build
from few textboxs (str=text1+text2...), how can i write the string to the
file in ASCII format?

2. I need my string will be 120 char length, and incase the string is less
then 120 chars, i need to fill it with spaces till it will be 120 chars
length, is there a better way then:

while(str.length<120)
{
str=str+" ";
}

Thanks,
Gidi.

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

1. Use a StreamWriter constructed with the proper System.Text.Encoding
instance (System.Text.Encoding.ASCII)
2. Use String.PadRight

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Gidi" <sh*****@hotmail.com.dontspam> wrote in message
news:5F**********************************@microsof t.com...
Hi,

1. I have a string that i want to write into a file. the string is build
from few textboxs (str=text1+text2...), how can i write the string to the
file in ASCII format?

2. I need my string will be 120 char length, and incase the string is less
then 120 chars, i need to fill it with spaces till it will be 120 chars
length, is there a better way then:

while(str.length<120)
{
str=str+" ";
}

Thanks,
Gidi.


Nov 17 '05 #2

P: n/a
Hi Dmytro:

Thanks for your fast answer.
I tried to use:
StreamWriter sw=new StreamWriter(string path,true,System.Text.Encoding.ASCII);
and now instead of hebrew letters i see in the file ???? (before that i saw
it as gibberish), do u have any idea why?

Thanks again,
Gidi.

"Dmytro Lapshyn [MVP]" wrote:
Hi,

1. Use a StreamWriter constructed with the proper System.Text.Encoding
instance (System.Text.Encoding.ASCII)
2. Use String.PadRight

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Gidi" <sh*****@hotmail.com.dontspam> wrote in message
news:5F**********************************@microsof t.com...
Hi,

1. I have a string that i want to write into a file. the string is build
from few textboxs (str=text1+text2...), how can i write the string to the
file in ASCII format?

2. I need my string will be 120 char length, and incase the string is less
then 120 chars, i need to fill it with spaces till it will be 120 chars
length, is there a better way then:

while(str.length<120)
{
str=str+" ";
}

Thanks,
Gidi.


Nov 17 '05 #3

P: n/a
ASCII does not allow for anything but Latin alphabet, numbers and special
symbols such as @ or #. You probably need a different encoding if you need
to write hebrew letters to a file.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Gidi" <sh*****@hotmail.com.dontspam> wrote in message
news:F5**********************************@microsof t.com...
Hi Dmytro:

Thanks for your fast answer.
I tried to use:
StreamWriter sw=new StreamWriter(string
path,true,System.Text.Encoding.ASCII);
and now instead of hebrew letters i see in the file ???? (before that i
saw
it as gibberish), do u have any idea why?

Thanks again,
Gidi.

"Dmytro Lapshyn [MVP]" wrote:
Hi,

1. Use a StreamWriter constructed with the proper System.Text.Encoding
instance (System.Text.Encoding.ASCII)
2. Use String.PadRight

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Gidi" <sh*****@hotmail.com.dontspam> wrote in message
news:5F**********************************@microsof t.com...
> Hi,
>
> 1. I have a string that i want to write into a file. the string is
> build
> from few textboxs (str=text1+text2...), how can i write the string to
> the
> file in ASCII format?
>
> 2. I need my string will be 120 char length, and incase the string is
> less
> then 120 chars, i need to fill it with spaces till it will be 120 chars
> length, is there a better way then:
>
> while(str.length<120)
> {
> str=str+" ";
> }
>
> Thanks,
> Gidi.
>



Nov 17 '05 #4

P: n/a
using (System.IO.StreamWriter sw =
new System.IO.StreamWriter("C:\Temp\fileName.txt", true,
System.Text.Encoding.ASCII))
{
sw.Write(str.PadRight(' ', 120));
}

The above example presupposes that (1) you want to create a new file, rather
than append to it, and (2) that you want to pad your string with spaces on
the right. Make sure to change it if either is not true, or, of course, if
you don't want to write to C:\Temp\filename.txt. ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Gidi" <sh*****@hotmail.com.dontspam> wrote in message
news:5F**********************************@microsof t.com...
Hi,

1. I have a string that i want to write into a file. the string is build
from few textboxs (str=text1+text2...), how can i write the string to the
file in ASCII format?

2. I need my string will be 120 char length, and incase the string is less
then 120 chars, i need to fill it with spaces till it will be 120 chars
length, is there a better way then:

while(str.length<120)
{
str=str+" ";
}

Thanks,
Gidi.

Nov 17 '05 #5

P: n/a
Gidi <sh*****@hotmail.com.dontspam> wrote:
Thanks for your fast answer.
I tried to use:
StreamWriter sw=new StreamWriter(string path,true,System.Text.Encoding.ASCII);
and now instead of hebrew letters i see in the file ???? (before that i saw
it as gibberish), do u have any idea why?


Yes - it was writing in UTF-8 before, it's now writing ASCII. ASCII
does not contain any Hebrew characters, so it's using '?' instead to
say that it can't write the character you want it to.

See http://www.pobox.com/~skeet/csharp/unicode.html for further
information about character sets in .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.