473,748 Members | 9,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using detectEncodingF romByteOrderMar ks while copying a text file

I've noticed after copying a text file line by line and comparing, that the
original had several bytes of data at the beginning denoting its encoding.
How do I use that in my copy?
My original code shown below, didn't produce a perfect copy, so I used the
StreamReader construct that includes detectEncodingF romByteOrderMar ks. But I
need to pass that to the construct for my StreamWriter so I need to be able
to work out the encoding type somehow. How please?

string InputPath = Path.GetDirecto ryName(Applicat ion.ExecutableP ath) +
@"\intext.tx t";
string OutputPath = Path.GetDirecto ryName(Applicat ion.ExecutableP ath)
+ @"\outtext.txt" ;
string In;
string Out;

using (StreamReader Input = new StreamReader(In putPath))
// using (StreamReader Input = new StreamReader(In putPath, true)) <<
construct
{
using (StreamWriter Output = new StreamWriter(Ou tputPath))
{
while ((In = Input.ReadLine( )) != null)
{
Out = DoSomethingTo(I n);
Output.WriteLin e(Out);
}
}
}

Jun 27 '08 #1
6 4968
I'm guessing - tell the writer about it?

using (StreamWriter Output = new StreamWriter(Ou tputPath, false,
Input.CurrentEn coding)) {...}

Marc
Jun 27 '08 #2
Correction - the CurrentEncoding is not valid until it has read some
data; perhaps something like below; note that it also can't detect every
encoding possible...

Marc

using (StreamReader reader = new StreamReader(pa th1, true))
{
string line = reader.ReadLine ();
using (StreamWriter writer = new StreamWriter(pa th2, false,
reader.CurrentE ncoding))
{
Console.WriteLi ne("Reading {0} with {1}", path1,
reader.CurrentE ncoding.Encodin gName);
Console.WriteLi ne("Writing {0} with {1}", path2,
writer.Encoding .EncodingName);

while (line != null)
{
string t = Transform(line) ;
Console.WriteLi ne(t);
writer.WriteLin e(t);
line = reader.ReadLine ();
}
}
}
Jun 27 '08 #3
"Marc Gravell" <ma**********@g mail.comwrote in message
news:u4******** ******@TK2MSFTN GP03.phx.gbl...
Correction - the CurrentEncoding is not valid until it has read some data;
perhaps something like below; note that it also can't detect every
encoding possible...
That's great! thank you :)

Jun 27 '08 #4
Using detectEncodingF romByteOrderMar ks while copying a text file
Unless you process the text somehow, it is not worth the trouble to
copy a text file as text file (with encoding detection, line ending,
and so on).
Just copy it as a binary. The routine can also be reused for any type
of files, and there is no risk of data corruption if you "guess" the
encoding wrong.
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #5
I very nearly said the same thing - but if you look carefully, there is
a transform hidden in the code:

Out = DoSomethingTo(I n);
Output.WriteLin e(Out);

Marc
Jun 27 '08 #6
I very nearly said the same thing - but if you look carefully, there is
a transform hidden in the code:
Right, I missed that one. Got fouled by the subject :-)
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #7

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

Similar topics

11
15794
by: Grant Edwards | last post by:
I'm trying in vain to set the icon for the executable generated by py2exe. According to various sources there are two answers: 1) Do it on the command line: python setup.py py2exe --icon foo.ico That generates a usage error: error: --icon not recognized
2
9764
by: Bernd Lambertz | last post by:
I have a problem with bcp and format files. We changed our databases from varchar to nvarchar to support unicode. No problems so fare with that. It is working fine. But now I need a format file for the customer table and and it is not working. It is working fine with the old DB with varchar, but with nvarchar I'm not able to copy the data. The biggest problem is, that I got no error message. BCP starts copying to table and finished...
22
6958
by: Matt | last post by:
When browsing a web page a user has the ability to highlight content on a page (by holding down the left mouse button and dragging the mouse over the desired content). Is there a way to disable this option? I assume there isn't but I have to try.
14
2358
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object in this case object b2 below. It also initialize subobject from class SphereClass. The BallClass copy constructor looks like this. BallClass::BallClass(const BallClass& bc) : SphereClass(bc)
0
2021
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application called gramps (http://www.gramps-project.org) for MAC OS X.
121
10130
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
3
1532
by: John | last post by:
Hi all, My application updates a sql server 2005 express database prior to copying it with the result being the "in use by another process" and I cannot copy it as a result. I've posted the code that updates the database below. Please could someone let me know how I free up the mdf file properly prior to copying it. Otherwise, does someone know how I can free it up programmatically or simply copy it without receiving the errror?
0
1484
by: Grant Edwards | last post by:
I've got a system where I try to install extensions using /usr/local/bin/python setup.py install But, it fails when it tries to use a non-existant compiler path and specs file. I suspect it's trying to use the compieler that was used to build /usr/bin/python. How do I get it to use the compiler settings that were used for /usr/local/bin/python?
6
4982
by: kimiraikkonen | last post by:
Hi, I use system.io.file class to copy files but i have a difficulty about implementing a basic / XP-like progress bar indicator during copying process. My code is this with no progress bar, or i couldn't find sth which give totalbytes/written bytes class. And does system.io.file class provide awaring of the chunks / bytes of the files bytes which are written?
0
9363
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
9312
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
8237
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
6793
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
4593
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
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.