Writing to file in ASCII format + one more question

G

Guest

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.
 
D

Dmytro Lapshyn [MVP]

Hi,

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

Guest

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 said:
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 said:
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.
 
D

Dmytro Lapshyn [MVP]

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 said:
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 said:
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 said:
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.
 
K

Kevin Spencer

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.
 
J

Jon Skeet [C# MVP]

Gidi said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top