Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Delete a line from a text file that contains Chinese characters.

Subject: Delete a line from a text file that contains Chinese characters.

From: Herwig Peters

Date: 25 Nov, 2010 12:28:05

Message: 1 of 3

Hi,

I want to delete a line from a text file. After I did a bit of reading on the net I found that most people simply suggest to read the whole file, only keep the lines you want to keep and then write these lines to another text file. This is a nice solution but in my case there is one problem: Chinese characters in the text and I want to keep them. But what happens is when I read the text file the encoding of these characters is lost and by the time I write the 'good' lines back to the other text file the characters are 'broken', ie I only see stuff like '*#ä?'.

Please don't get me wrong, the ultimate aim is NOT to read the Chinese characters but to delete certain lines from a text file that contains Chinese characters. Any suggestions on how I can achieve this are most welcome!

Thanks
Herwig

PS: My computer system is NOT Chinese but English, I myself am not fluent in Chinese either.

Subject: Delete a line from a text file that contains Chinese characters.

From: Walter Roberson

Date: 26 Nov, 2010 16:41:25

Message: 2 of 3

On 25/11/10 6:28 AM, Herwig Peters wrote:

> I want to delete a line from a text file. After I did a bit of reading
> on the net I found that most people simply suggest to read the whole
> file, only keep the lines you want to keep and then write these lines to
> another text file.

Yes, that is correct.

> This is a nice solution but in my case there is one
> problem: Chinese characters in the text and I want to keep them. But
> what happens is when I read the text file the encoding of these
> characters is lost and by the time I write the 'good' lines back to the
> other text file the characters are 'broken', ie I only see stuff like
> '*#ä?'.

After you

fid = fopen(InputFileName, 'rt');

do

[filename, permission, machineformat, encoding] = fopen(fid);

in order to find out which encoding the original file used. Then when
you fopen() the new file for writing, specify the same machineformat and
encoding the input file had.

Subject: Delete a line from a text file that contains Chinese characters.

From: Herwig Peters

Date: 27 Nov, 2010 08:22:05

Message: 3 of 3

Thanks a lot! That did the trick!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us