473,666 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about reading an UTF-8 text file

Hi, everyone. The other day I wrote a piece of code to see how the
UTF-8 text file was saved in the memory. Although the code worked
seemingly well, I still cannot understand a problem about the output
format.

The code is like this (I ignored the #include and #define here) :
int main(void)
{
FILE* fp = NULL;
char buffer[MAX_SIZE];
int byteRead = 0;
char* cp = NULL;

memset(buffer, '\0', sizeof(buffer)) ;

if ((fp = fopen(FILE_NAME , "r")) != NULL)
{
byteRead = fread(buffer, sizeof(char), MAX_SIZE, fp);
if (byteRead != 0)
{
cp = (char *)buffer;
while ((*cp) != '\0')
{
printf("%#X\t", *cp++);
}
}
else
{
}
fclose(fp);
}
else
{
perror("open");
}
return 0;
}

The UTF-8 text file it reads in contains five Japanese characters
("あいうえ ") and four ascii character ("abcd"). And the output
is as below:

0XFFFFFFEF 0XFFFFFFBB 0XFFFFFFBF 0XFFFFFFE3
0XFFFFFF81
0XFFFFFF82 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF84
0XFFFFFFE3
0XFFFFFF81 0XFFFFFF86 0XFFFFFFE3 0XFFFFFF81
0XFFFFFF88
0XFFFFFFE3 0XFFFFFF81 0XFFFFFF8A 0X61 0X62 0X63
0X64

Well,The result is fine, but I don't understand how do those "FFFFFF"
come out? In my opinion, printf("%#X\t", *cp++) will only output the
content of one byte, but the output seems like four bytes. I don't know
why. Can anybody tell my how does it happen? Thanx~~~

Mar 18 '06 #1
8 2716
On 17 Mar 2006 21:58:23 -0800, "Claude Yih" <wi******@gmail .com>
wrote:
Hi, everyone. The other day I wrote a piece of code to see how the
UTF-8 text file was saved in the memory. Although the code worked
seemingly well, I still cannot understand a problem about the output
format.

The code is like this (I ignored the #include and #define here) :
int main(void)
{
FILE* fp = NULL;
char buffer[MAX_SIZE];
int byteRead = 0;
char* cp = NULL;

memset(buffer, '\0', sizeof(buffer)) ;

if ((fp = fopen(FILE_NAME , "r")) != NULL)
{
byteRead = fread(buffer, sizeof(char), MAX_SIZE, fp);
if (byteRead != 0)
{
cp = (char *)buffer;
The cast is superfluous.
while ((*cp) != '\0')
{
printf("%#X\t", *cp++);
%x requires an unsigned int. On your system, char is apparently
signed. Try changing cp to an unsigned char* and buffer to an
unsigned char array.
}
}
else
{
}
If you don't have any else action, you don't need the else at all.
fclose(fp);
}
else
{
perror("open");
}
return 0;
}

The UTF-8 text file it reads in contains five Japanese characters
("?????") and four ascii character ("abcd"). And the output
is as below:

0XFFFFFFEF 0XFFFFFFBB 0XFFFFFFBF 0XFFFFFFE3
0XFFFFFF81
0XFFFFFF82 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF84
0XFFFFFFE3
0XFFFFFF81 0XFFFFFF86 0XFFFFFFE3 0XFFFFFF81
0XFFFFFF88
0XFFFFFFE3 0XFFFFFF81 0XFFFFFF8A 0X61 0X62 0X63
0X64

Well?The result is fine, but I don't understand how do those "FFFFFF"
come out? In my opinion, printf("%#X\t", *cp++) will only output the
content of one byte, but the output seems like four bytes. I don't know
why. Can anybody tell my how does it happen? Thanx~~~


Since printf is a variadic function, all integer arguments smaller
than int (unsigned int) are promoted to int (or unsigned int). Think
about why 0x81 has the Fs but 0x61 doesn't.

Similarly, all floats would be promoted to double.
Remove del for email
Mar 18 '06 #2
"Claude Yih" <wi******@gmail .com> writes:
Hi, everyone. The other day I wrote a piece of code to see how the
UTF-8 text file was saved in the memory. Although the code worked
seemingly well, I still cannot understand a problem about the output
format.

The code is like this (I ignored the #include and #define here) :
int main(void)
{

[snip]

Why would you want to ignore the #include and #define directives?
Your program is invalid without them. If you're going to post a
program, post the whole thing (unless it's too big to post).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 18 '06 #3
Claude Yih wrote:
Hi, everyone. The other day I wrote a piece of code to see how the
UTF-8 text file was saved in the memory. Although the code worked
seemingly well, I still cannot understand a problem about the output
format.

The code is like this (I ignored the #include and #define here) :
I won't ignore #includes and #defines. It is expected that code posted
here be compilable. The remainder of Claude Yih's question follows this
note.
See what the following does on your implementation. Note the 'unsigned'.

#include <stdio.h>
#define FILE_NAME "./japchar"

int main(void)
{
FILE *fp = NULL;
unsigned char buffer[BUFSIZ], *cp;

if ((fp = fopen(FILE_NAME , "rb"))) {
if (fread(buffer, 1, BUFSIZ, fp))
for (cp = buffer; *cp; cp++)
printf("%#X\t", *cp);
putchar('\n');
fclose(fp);
}
else
perror("open");

return 0;
}

[output for my implementation with ./japchar containing the characters
あいうえお + EOL characters]

0XE3 0X81 0X82 0XE3 0X81 0X84 0XE3 0X81 0X86
0XE3 0X81 0X88 0XE3 0X81 0X8A 0XD 0XA



int main(void)
{
FILE* fp = NULL;
char buffer[MAX_SIZE];
int byteRead = 0;
char* cp = NULL;

memset(buffer, '\0', sizeof(buffer)) ;

if ((fp = fopen(FILE_NAME , "r")) != NULL)
{
byteRead = fread(buffer, sizeof(char), MAX_SIZE, fp);
if (byteRead != 0)
{
cp = (char *)buffer;
while ((*cp) != '\0')
{
printf("%#X\t", *cp++);
}
}
else
{
}
fclose(fp);
}
else
{
perror("open");
}
return 0;
}

The UTF-8 text file it reads in contains five Japanese characters
("あいうえ ") and four ascii character ("abcd"). And the output
is as below:

0XFFFFFFEF 0XFFFFFFBB 0XFFFFFFBF 0XFFFFFFE3
0XFFFFFF81
0XFFFFFF82 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF84
0XFFFFFFE3
0XFFFFFF81 0XFFFFFF86 0XFFFFFFE3 0XFFFFFF81
0XFFFFFF88
0XFFFFFFE3 0XFFFFFF81 0XFFFFFF8A 0X61 0X62 0X63
0X64

Well,The result is fine, but I don't understand how do those "FFFFFF"
come out? In my opinion, printf("%#X\t", *cp++) will only output the
content of one byte, but the output seems like four bytes. I don't know
why. Can anybody tell my how does it happen? Thanx~~~

Mar 18 '06 #4
Yeh, referring to the <Computer Systems - A Programmer's Perspective>,
I knew why I should change the char into unsigned char here. Thanx,
buddies :)

P.S: Next time, I won't ignore #include and #define any more :) Thanks
for reminding me of that.

Mar 18 '06 #5
Claude Yih opined:
Yeh, referring to the <Computer Systems - A Programmer's
Perspective>, I knew why I should change the char into
unsigned char here. Thanx, buddies :)
But we don't, as you didn't post any context. Read:

<http://cfaj.freeshell. org/google/>
<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>
P.S: Next time, I won't ignore #include and #define any more
:) Thanks for reminding me of that.


Also, don't forget to follow good advice at the above URLs.

--
BR, Vladimir

For adult education nothing beats children.

Mar 18 '06 #6
<http://cfaj.freeshell. org/google/>
<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>
Also, don't forget to follow good advice at the above URLs.


I'd admit those URLs helped a lot. I'll pay attention to that when I
post another message next time.

Mar 19 '06 #7
Claude Yih opined:
<http://cfaj.freeshell. org/google/>
<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>


Also, don't forget to follow good advice at the above URLs.


I'd admit those URLs helped a lot. I'll pay attention to that when I
post another message next time.


Thank you!

--
BR, Vladimir

Your business will go through a period of considerable expansion.

Mar 19 '06 #8
Claude Yih wrote:
<http://cfaj.freeshell. org/google/>
<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

Also, don't forget to follow good advice at the above URLs.


I'd admit those URLs helped a lot. I'll pay attention to that
when I post another message next time.


Good. Here are some more useful links.

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Mar 19 '06 #9

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

Similar topics

1
2837
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
8
2458
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
38
5730
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I find an answer to this question (don't find it in the W3C_char_entities document). -- Haines Brown brownh@hartford-hwp.com
5
2993
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing
5
1588
by: Pavils Jurjans | last post by:
Hello, Here's an excerpt from msdn online documentation: An index is the position of a Char, not a Unicode character, in a String. An index is a zero-based, nonnegative number starting from the first position in the string, which is index position zero. Consecutive index values might not correspond to consecutive Unicode characters because a Unicode character might be encoded as more than one Char. To work with each Unicode character...
23
8185
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString, etc. There are also char, wchar_t, QChar, XMLCh, etc., for character representation. Coming from Java where a String is a String is a String, that was quite a shock. Well, I'm back to looking at this, and it still isn't pretty. I've found...
3
6737
by: =?GB2312?B?1qP2zrrA?= | last post by:
hi all, i need to covert the utf8 character to gbk,is that possible?Any idea will be appreciate.
7
6840
by: Elliot | last post by:
My XML is using encoding UTF-8 and its content contains Chinese character. When debug the following codes: string strXmlFile = "xml.xml"; XmlDocument objXml = new XmlDocument(); objXml.Load(strXmlFile); It returns "Invalid character in the given encoding" and point to objXml.Load(strXmlFile);.
160
5609
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
8448
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8356
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,...
0
8871
Oralloy
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...
1
8552
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
7387
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 projectplanning, coding, testing, and deploymentwithout 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
6198
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
4198
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...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.