emoji displaying and handling problem
Show older comments
Recently i'm working on processing txt files containing some emojis and meet some problems in file reading.
For demosntration, I put some random emojis in test.txt and use fopen and fread to read its contens.
The txt file is saved in UTF-8 and CRLF mode.
i use
fin = fopen('test.txt', 'rb', 'n', 'UTF-8');
content = fscanf(fin, '%c');
fclose(fin);
disp(content);
only some of the emojis displays correctly (as shown below).

i tried change read option from 'rb' to 'r'
or
fin = fopen('test.txt', 'rb', 'n', 'UTF-8');
while ~feof(fin)
content = fgetl(fin);
disp(content);
end
fclose(fin);
or
fin = fopen('test.txt', 'rb', 'n', 'UTF-8');
content = fread(fin, '*char')';
fclose(fin);
disp(content);
They all don't work. So i can't continue writting, save or other operations targeted on 'content'.
fprintf('%s',content);
doesn't show correctly too.
I also tried saving test.txt encoding to UTF-8 with BOM or CL, still no differnece.
Another test
fid = fopen('test.txt','w','n','UTF-8');
cotents = "🆘🅾️🆚✴️🈹☸️✡️♈⛎🈷️🫠📊🌚😈Ⓜ️🎬➡️⬇️";
fprintf(fid,"%s",cotents);
fclose(fid);
fid = fopen('test.txt','r','n','UTF-8');
content = fscanf(fid, '%c');
fclose(fid);
disp(content);
The above code handles writting correctly but fails in reading.
It seems it's not related to display or fprintf or sprinf, the contents is already changed after reading operation.
Grealy appreciated if someone helps.
How can i read and store these characters properly.
Again, thanks.
P.S. Here is my feature('locale') output:
ctype: 'zh_CN.UTF-8'
collate: 'zh_CN.UTF-8'
time: 'zh_CN.UTF-8'
numeric: 'en_US_POSIX.UTF-8'
monetary: 'zh_CN.UTF-8'
messages: 'zh_CN.UTF-8'
encoding: 'UTF-8'
terminalEncoding: 'GBK'
jvmEncoding: 'UTF-8'
status: 'MathWorks locale management system initialized.'
warning: ''
Accepted Answer
More Answers (0)
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!