Hi,
I have a small program which opens a file (Whose content is a mac
address in the form xx:xx:xx:xx:xx: xx) and uses fscanf() to get
individual bytes into an array which is defined as "unsigned char
macaddress[6]". The strange thing is that it prints zeroes on MIPS
processor where as it works correctly on x86 systems. Appreciate if
some body could help to understand the fscanf() behavior.
Here is the code I am using:
#include <stdio.h>
main()
{
FILE *fp;
unsigned char macaddress[6];
char buf[50];
fp = fopen("/tmp/macaddress", "r" );
if (fp == NULL ) {
printf("Couldn' t open the file \n");
return -1;
}
fscanf(buf,"%x: %x:%x:%x:%x:%x" ,
&macaddress[0],&macaddress[1],&macaddress[2],&macaddress[3],&macaddress[4],&macaddress[5]);
printf("MAC address: %x:%x:%x:%x:%x: %x\n",macaddres s[0],
macaddress[1], macaddress[2], macaddress[3], macaddress[4],
macaddress[5]);
fclose(fp);
}
Thanks in Advance
--gb 5 1903
"gb" <gb*****@gmail. com> wrote:
<snip> Here is the code I am using:
#include <stdio.h>
main() { FILE *fp; unsigned char macaddress[6]; char buf[50];
fp = fopen("/tmp/macaddress", "r" ); if (fp == NULL ) { printf("Couldn' t open the file \n"); return -1; } fscanf(buf,"%x: %x:%x:%x:%x:%x" , &macaddress[0],&macaddress[1],&macaddress[2],&macaddress[3],&macaddress[4],&macaddress[5]); printf("MAC address: %x:%x:%x:%x:%x: %x\n",macaddres s[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); fclose(fp); }
Besides some other minor issues with your code (e.g. implicit int;
unportable return code; failure to check return value of fscanf),
you most certainly want to reread your library documentation
for proper invocation of the fscanf function. ;-)
Thanks in Advance
HTH :)
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
On Thu, 08 Sep 2005 03:57:50 -0700, gb wrote: Hi,
I have a small program which opens a file (Whose content is a mac address in the form xx:xx:xx:xx:xx: xx) and uses fscanf() to get individual bytes into an array which is defined as "unsigned char macaddress[6]". The strange thing is that it prints zeroes on MIPS processor where as it works correctly on x86 systems. Appreciate if some body could help to understand the fscanf() behavior.
Your code invokes undefined behaviour so anything can happen and it is
reasonable for different things to happen on different implementations .
Here is the code I am using:
#include <stdio.h>
main()
Make that
int main(void)
for good stype and to be compatible with current definitions of C.
{ FILE *fp; unsigned char macaddress[6]; char buf[50];
fp = fopen("/tmp/macaddress", "r" ); if (fp == NULL ) { printf("Couldn' t open the file \n"); return -1;
The standard defined exit() values (and alse return values from main())
are 0, EXIT_SUCCESS and EXIT_FAILURE. The last 2 are macrosd defined in
<stdlib.h>.
} fscanf(buf,"%x: %x:%x:%x:%x:%x" , &macaddress[0],&macaddress[1],&macaddress[2],&macaddress[3],&macaddress[4],&macaddress[5]);
The %x fcanf() format specifier requires a corresponding argument of
unsigned * (i.e. it writes to an unsigned int object) but you're passing
it a pointer to unsigned char.
In C99 you can fix this by using %hhx instead of %x. If you want to stay
compatible with C90 you could write the converted values to an array of
unsigned int, then copy then to the array of unsigned char afterwards.
printf("MAC address: %x:%x:%x:%x:%x: %x\n",macaddres s[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
This is OK, because the daefault argument promotions will convery unsigned
char arguments to int (or in rare circonstances unsigned int) which is OK
for %x. There are no such conversions pointers, nor could there be because
the pointer has to have the appropriate type for the object being pointed
at.
fclose(fp);
return 0;
}
Lawrence
Hi,
Thanks a lot for your comments. I agree that my code was not very
portable and elegant as it was just a test program. Appreciate your
useful suggestions.
I was able to fix the problem by using "%hhx" instead of "%x".
Thanks once again
--gb
"gb" <gb*****@gmail. com> wrote:
<snip> I was able to fix the problem by using "%hhx" instead of "%x".
<snip>
The hh length modifier was added in C99, you're indeed lucky if you
have a C99 conforming library at hand. If portability is important
for your project, you might want to think about a workaround, e.g.
fetch input in terms of int and cast to unsigned char.
Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Phung |
last post by:
Is there a fscanf equivalent for c++? Here's what I'm talking about:
unsigned int NextAddress(ifstream& AddressFile) {
unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream?
-->fscanf(AddressFile, "%x", next_address);
if(AddressFile.eof()) {
return 0;
|
by: Mantorok Redgormor |
last post by:
I have failed to find an authoritive answer on the following:
fscanf(NULL, ..);
The rest of the function arguments have been omitted.
I'm only curious as to what behavior is given when
the first argument is NULL?
I went through J.2 of the standard but it isn't listed as
|
by: Mantorok Redgormor |
last post by:
#include <stdio.h>
struct foo { int example; struct bar *ptr; };
int main(void)
{
struct foo baz;
baz.ptr = NULL; /* Undefined behavior? */
return 0;
|
by: Thomas Sourmail |
last post by:
Hi,
I hope I am missing something simple, but.. here is my problem:
I need my program to check the last column of a file, as in :
a b c d target ref
0 0 0 0 1 a
1 0 0 0 1.5 b
2 0 0 0 2 c
|
by: Kay |
last post by:
1) If i want to read data from a txt file,
eg John; 23; a
Mary; 16; i
How can I read the above data stopping reading b4 each semi-colon and
save it in three different variables ?
2) If I enter a number, can I use to call a particular node ?
eg enter a number: 3
calling node of number 3
is it possible ?
| |
by: siliconwafer |
last post by:
Hi All,
here is one code:
int main() {
FILE*fp;
unsigned long a;
fp = fopen("my_file.txt","w+");
a = 24;
fprintf(fp,"%ld",a);
while(fscanf(fp,"%ld",&a) == 1) {
|
by: Gary Baydo |
last post by:
The following program illustrates a question I have about fscanf()
behavior:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *testfile;
char s1 = "01234567890123456789";
|
by: subirs |
last post by:
Hi,
I am encountering SIGSEGV if I am opeing and closing a file in a
do-while loop. i am including the part of the code where I have used
fscanf.
-----------------------------------------------------------------
FILE *save;
do
{
save=fopen("Settings/iwrite.dat","r");
|
by: PeterOut |
last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
I am not sure if this is a C, C++ or MS issue but fscanf has been
randomly hanging on me. I make the call hundreds, if not thousands,
of times but it hangs in different places with the same data. The
offending code follows.
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
|
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: 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...
|
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: 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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |