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);
}
}
} 6 4961
I'm guessing - tell the writer about it?
using (StreamWriter Output = new StreamWriter(Ou tputPath, false,
Input.CurrentEn coding)) {...}
Marc
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 ();
}
}
}
"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 :)
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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.
|
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)
|
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.
| |
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...
|
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?
|
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?
|
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?
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |