473,562 Members | 2,956 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 32869
<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
17601
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
2447
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...
3
7743
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...
8
4026
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...
11
3636
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...
4
40208
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...
5
18633
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...
2
7520
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...
0
1605
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...
0
7655
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
7577
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...
1
7627
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
7935
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
6221
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...
1
5477
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...
0
5193
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3623
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...
0
903
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.