473,725 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a .txt file with unicode encoding

Hey guys,
Can anyone tell me how to create a text file with Unicode Encoding. In
am using

FileStream Finalfile = new FileStream("fin alfile.txt",
FileMode.Append , FileAccess.Writ e);

###Question:
Now this creates finalfile.txt with ANSI Encoding ...which is a
default. Either tell me how to change the default or how to create a
new file with ecoding already set to unicode.

###Background:
I am already using the work around by which i create a finalfile.txt
manually and then 'save as...' in unicode Encoding. It doesnt work
though. :(
Basically i need to write English and Japanese characters in the same
file and then use it. If i have a file say finalfile.txt with unicode
encoding and i try to write into it using this code:

FinalStream.Wri teLine(EngStrin g + "is written as" + JapaneseString
+"\n");

I am using Encoding for code page 932. And unfortunately i get same
results for any other encoding say 1252 as long as the txt file that i
use as a work-around has unicode Encoding. I dnt understand why it
always takes Japanese characters.

###The Code:

/
*************** *************** *************** *************** *************** *************/
FileStream Engfile = new FileStream("eng file.txt", FileMode.Open,
FileAccess.Read );

FileStream Japanesefile = new FileStream("Jap anesefile.txt",
FileMode.Open, FileAccess.Read );
FileStream Finalfile = new FileStream("fin alfile.txt",
FileMode.Append , FileAccess.Writ e);

StreamReader EngStream = new StreamReader(En gfile,Targetenc oding);
StreamReader JapaneseStream = new
StreamReader(Ja panesefile,Targ etencoding);
StreamWriter FinalStream = new StreamWriter(Fi nalfile,Targete ncoding);
string EngString = null;
while ((EngString = EngStream.ReadL ine()) != null)
{
string JapaneseString = JapaneseStream. ReadLine();
FinalStream.Wri teLine(EngStrin g + "~" + JapaneseString +"\n");
}
/
*************** *************** *************** *************** *************** *************/
TargetEncoding is set as 932 earlier in the application.

Mar 27 '07 #1
1 32929
<uj***********@ gmail.comschrie b im Newsbeitrag
news:11******** **************@ o5g2000hsb.goog legroups.com...
Hey guys,
Can anyone tell me how to create a text file with Unicode Encoding. In
am using

FileStream Finalfile = new FileStream("fin alfile.txt",
FileMode.Append , FileAccess.Writ e);

###Question:
Now this creates finalfile.txt with ANSI Encoding ...which is a
default. Either tell me how to change the default or how to create a
new file with ecoding already set to unicode.
No, this doesn't create a textfile in any encoding. It simply creates an
empty file with the name "finalfile.txt" .
A file for itself doesn't have any encoding. There can be text stored in it
with a certain encoding, but without knowing the encoding used on writing
the file, there is no way of determining the encoding other than opening the
file with a guessed encoding and looking if the content is any readable test
and has no odd characters in it.
>
###Background:
I am already using the work around by which i create a finalfile.txt
manually and then 'save as...' in unicode Encoding. It doesnt work
though. :(
Basically i need to write English and Japanese characters in the same
file and then use it. If i have a file say finalfile.txt with unicode
encoding and i try to write into it using this code:

FinalStream.Wri teLine(EngStrin g + "is written as" + JapaneseString
+"\n");

I am using Encoding for code page 932. And unfortunately i get same
results for any other encoding say 1252 as long as the txt file that i
use as a work-around has unicode Encoding. I dnt understand why it
always takes Japanese characters.

###The Code:

/
*************** *************** *************** *************** *************** *************/
FileStream Engfile = new FileStream("eng file.txt", FileMode.Open,
FileAccess.Read );

FileStream Japanesefile = new FileStream("Jap anesefile.txt",
FileMode.Open, FileAccess.Read );
FileStream Finalfile = new FileStream("fin alfile.txt",
FileMode.Append , FileAccess.Writ e);

StreamReader EngStream = new StreamReader(En gfile,Targetenc oding);
Here the encoding should be given, with wich the english file was written.
StreamReader JapaneseStream = new
StreamReader(Ja panesefile,Targ etencoding);
Here the encoding should be given, with wich the japanese file was written.
StreamWriter FinalStream = new StreamWriter(Fi nalfile,Targete ncoding);
Here should the encoding be given, in wich the text should be stored in the
final file. This would be Encoding.Unicod e for your case. (btw
Encoding.Unicod e is UTF16; Unicode itself is no encoding, but a charactermap
for wich different encoding exist.)

Since you're using the same Encoding for all three files, the content can't
be changed. Only maybe some bytes are swollowed wich aren't valid in that
encoding, (if ther are any.)

I hope this helps you better understand encodings and solve your problem.
Christof
>

string EngString = null;
while ((EngString = EngStream.ReadL ine()) != null)
{
string JapaneseString = JapaneseStream. ReadLine();
FinalStream.Wri teLine(EngStrin g + "~" + JapaneseString +"\n");
}
/
*************** *************** *************** *************** *************** *************/
TargetEncoding is set as 932 earlier in the application.

Mar 27 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17618
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code that - opens a file appropriately for output - writes to this file Thanks very much. Michael Weir
4
2460
by: Majed | last post by:
Hi , all I'm trying to write unicode to a file for another app (not developed with vs2003) to read it. I used StreamWriter with unicode encoding.but I was surprised that the streamwriter adds FFFE to the start of the file,which stopes the other app from reading it!! any idea how to stope it frome doing that,do I have to use another class #####writer that supports unicode? help me Please! Thanks
3
7772
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a DB and display it onscreen. No matter which way I open the file, convert it to Unicode/leave it as is or what ever, I see all single bytes ok, but double bytes become 2 seperate single bytes. Surely there is an easy way to convert these mixed...
8
4062
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my surprise, I noticed that there was a space between every character as I outputted the lines to the...
11
3659
by: Patrick Van Esch | last post by:
Hello, I have the following problem of principle: in writing HTML pages containing ancient greek, there are two possibilities: one is to write the unicode characters directly (encoded as two bytes) into the HTML source, and save this source not as an ASCII text, but as a UNICODE text file (using 16 bits per character, also for the Western ASCII characters, which are usually encoded as Ox00XX with XX the ASCII code) ; or to write a pure...
4
40989
by: ferg | last post by:
I've checked all the FAQs and help - spent a couple of hours on this now and it's really bugging me. I've got Moin 1.3.5 installed on IIS 5.0. I can view and edit pages without any problems, but cannot create new pages - either trough the GUI, or by creating a new WikiName and clicking on it. I'm stumped as to whether this is a Moin ACL problem (played around a lot with this, but to no avail) or perhaps an IIS config problem (though...
5
18662
by: Jamie | last post by:
I have a file that was written using Java and the file has unicode strings. What is the best way to deal with these in C? The file definition reads: Data Field Description CHAR File identifier (64 bytes corresponding to Unicode character string padded with '0' Unicode characters. CHAR File format version (32 bytes corresponding to Unicode character string "x.y.z" where x, y, z are integers corresponding to major, minor and...
2
7532
by: starffly | last post by:
I want to read a xml file in Unicode, UTF-8 or a native encoding into a wchar_t type string, so i write a routine as follows, however, sometimes a Unicode file including Chinese character cannot be read completely. and I cannot tell where its root located, so NEED your help, GIVE me a hand please. THX. static Status LoadXMLFile2String(const char *filename, wchar_t *text){ FILE *f; if(!(f = fopen(filename, "r"))){ __printDebugA("Input...
0
1622
by: UvT | last post by:
Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); Now this creates finalfile.txt with ANSI Encoding ...which is a default. Either tell me how to change the default or how to create a new file with ecoding already set to unicode. I am already using the work around by which i create a finalfile.txt myself and then 'save...
0
9401
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.