473,549 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing to file in ASCII format + one more question

Hi,

1. I have a string that i want to write into a file. the string is build
from few textboxs (str=text1+text 2...), 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.lengt h<120)
{
str=str+" ";
}

Thanks,
Gidi.

Nov 17 '05 #1
5 3605
Hi,

1. Use a StreamWriter constructed with the proper System.Text.Enc oding
instance (System.Text.En coding.ASCII)
2. Use String.PadRight

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

1. I have a string that i want to write into a file. the string is build
from few textboxs (str=text1+text 2...), 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.lengt h<120)
{
str=str+" ";
}

Thanks,
Gidi.


Nov 17 '05 #2
Hi Dmytro:

Thanks for your fast answer.
I tried to use:
StreamWriter sw=new StreamWriter(st ring path,true,Syste m.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.Enc oding
instance (System.Text.En coding.ASCII)
2. Use String.PadRight

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

1. I have a string that i want to write into a file. the string is build
from few textboxs (str=text1+text 2...), 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.lengt h<120)
{
str=str+" ";
}

Thanks,
Gidi.


Nov 17 '05 #3
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*****@hotmai l.com.dontspam> wrote in message
news:F5******** *************** ***********@mic rosoft.com...
Hi Dmytro:

Thanks for your fast answer.
I tried to use:
StreamWriter sw=new StreamWriter(st ring
path,true,Syste m.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.Enc oding
instance (System.Text.En coding.ASCII)
2. Use String.PadRight

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Gidi" <sh*****@hotmai l.com.dontspam> wrote in message
news:5F******** *************** ***********@mic rosoft.com...
> Hi,
>
> 1. I have a string that i want to write into a file. the string is
> build
> from few textboxs (str=text1+text 2...), 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.lengt h<120)
> {
> str=str+" ";
> }
>
> Thanks,
> Gidi.
>



Nov 17 '05 #4
using (System.IO.Stre amWriter sw =
new System.IO.Strea mWriter("C:\Tem p\fileName.txt" , true,
System.Text.Enc oding.ASCII))
{
sw.Write(str.Pa dRight(' ', 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\filenam e.txt. ;-)

--
HTH,

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

"Gidi" <sh*****@hotmai l.com.dontspam> wrote in message
news:5F******** *************** ***********@mic rosoft.com...
Hi,

1. I have a string that i want to write into a file. the string is build
from few textboxs (str=text1+text 2...), 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.lengt h<120)
{
str=str+" ";
}

Thanks,
Gidi.

Nov 17 '05 #5
Gidi <sh*****@hotmai l.com.dontspam> wrote:
Thanks for your fast answer.
I tried to use:
StreamWriter sw=new StreamWriter(st ring path,true,Syste m.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.co m>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2020
by: Albert Tu | last post by:
Hi, I am learning and pretty new to Python and I hope your guys can give me a quick start. I have an about 1G-byte binary file from a flat panel x-ray detector; I know at the beggining there is a 128-byte header and the rest of the file is integers in 2-byte format. What I want to do is to save the binary data into several smaller files
19
5640
by: Svennglenn | last post by:
I'm working on a program that is supposed to save different information to text files. Because the program is in swedish i have to use unicode text for ÅÄÖ letters. When I run the following testscript I get an error message. # -*- coding: cp1252 -*-
4
6403
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of points = 123 @@@begin data@@@ gibberish follows.....
2
3894
by: JamesT | last post by:
Does anyone know how to get the information from a Wave File Header in VB.NET (or VB6). I need to check the sample rate for the correct value. JamesT
2
3056
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an array). I cannot change anything in the program but can add some features by which I can convert this array of data into a file. The easiest thing...
21
4666
by: siroregano | last post by:
Hi Everyone- I'm new to this group, and almost-as-new to asking programming questions publicly, so please forgive me if I miss a convention or two! I have a text file, around 40,000 lines long, where each line is a string of 4 ASCII characters corresponding to a 12-bit hexadecimal audio sample. The file reads something like this... 081F
8
18642
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in adv.
3
3983
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user selects? thanks...
7
1634
by: tshad | last post by:
I have a program in 2005 that is reading a text file removing text and then writing it back out again. It removes lines that start with PRINT. This program has worked fine for months. Now all of a sudden, it is reading a straight text file and adding a null after each character it reads in. Why is that? The original file doesn't have...
0
7521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.