473,791 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Warning in data format of the fscanf function

Hi,
I hope to be not off topic..
I have a string on a file "00:32:43:54:A2 :2D" (let's say a MAC
address)
I use fscanf like this
fscanf( file, "%02X:%02X:%02X :%02X:%02X:%02X ",
ptr, ptr+1, ptr+2,
ptr3, ptr+4, ptr+5);
It works, but I get warnings as the expected size is int while I'm
storing onto a uint8_t array.. is there a format to store onto a
uint8_t array instead of using a int array? ( I mean to don't get any
warning)
Things are working but I hate that warnings..
Thanks

Aug 7 '07 #1
6 3732
On Tue, 07 Aug 2007 10:11:38 +0000, InuY4sha wrote:
Hi,
I hope to be not off topic..
I have a string on a file "00:32:43:54:A2 :2D" (let's say a MAC
address)
I use fscanf like this
fscanf( file, "%02X:%02X:%02X :%02X:%02X:%02X ",
ptr, ptr+1, ptr+2,
ptr3, ptr+4, ptr+5);
It works, but I get warnings as the expected size is int while I'm
storing onto a uint8_t array.. is there a format to store onto a
uint8_t array instead of using a int array? ( I mean to don't get any
warning)
Things are working but I hate that warnings..
They are working because you're lucky enough. "Something" tells me
that your machine is little endian. (Figuring out what this
"something" is is left as an exercise.)

Use the macro SCNx8 in <inttypes.hwhic h expands to a string
literal, like this:
fscanf(file, "%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8
":%02" SCNx8 ":%02" SCNx8, ptr, ptr+1, ptr+2, ptr+3,
/* you didn't actually mean ptr3, did you? */, ptr+4, ptr+5);

This work due to string concatenation. For example, if SCNx8
expands to "hhx", "%02" SCNx8 ":%02" expands to "%02" "hhx" ":%02"
which is equivalent to "%02hhx:%02 ".
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 7 '07 #2
On 7 Ago, 12:46, Army1987 <army1...@NOSPA M.itwrote:
On Tue, 07 Aug 2007 10:11:38 +0000, InuY4sha wrote:
Hi,
I hope to be not off topic..
I have a string on a file "00:32:43:54:A2 :2D" (let's say a MAC
address)
I use fscanf like this
fscanf( file, "%02X:%02X:%02X :%02X:%02X:%02X ",
ptr, ptr+1, ptr+2,
ptr3, ptr+4, ptr+5);
It works, but I get warnings as the expected size is int while I'm
storing onto a uint8_t array.. is there a format to store onto a
uint8_t array instead of using a int array? ( I mean to don't get any
warning)
Things are working but I hate that warnings..

They are working because you're lucky enough. "Something" tells me
that your machine is little endian. (Figuring out what this
"something" is is left as an exercise.)

Use the macro SCNx8 in <inttypes.hwhic h expands to a string
literal, like this:
fscanf(file, "%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8
":%02" SCNx8 ":%02" SCNx8, ptr, ptr+1, ptr+2, ptr+3,
/* you didn't actually mean ptr3, did you? */, ptr+4, ptr+5);

This work due to string concatenation. For example, if SCNx8
expands to "hhx", "%02" SCNx8 ":%02" expands to "%02" "hhx" ":%02"
which is equivalent to "%02hhx:%02 ".
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)
This way I don't get the warning but it doesn't seem to be working
fine as the data retrieved is wrong... (while before it was fine..)
>From what I see the printed data is totally random.
Aug 7 '07 #3
This way I don't get the warning but it doesn't seem to be working
fine as the data retrieved is wrong... (while before it was fine..)
From what I see the printed data is totally random.
Sorry it was my mistake... I did as you said and it worked.. as it
worked with %02hhX... why shouldn't I use this last solution instead..
as it-s reported in the man page of fscanf ?

Aug 7 '07 #4
On Tue, 07 Aug 2007 12:03:56 +0000, InuY4sha wrote:
>
>This way I don't get the warning but it doesn't seem to be working
fine as the data retrieved is wrong... (while before it was fine..)
>From what I see the printed data is totally random.

Sorry it was my mistake... I did as you said and it worked.. as it
worked with %02hhX... why shouldn't I use this last solution instead..
as it-s reported in the man page of fscanf ?
It works because uint8_t is unsigned char, and hhX is a conversion
specifier for unsigned char. (I think it is very unlikely that
there is a system where uint8_t exists but it is not unsigned
char, though I think the standard does allow it to be an extended
type with the same size and range, even if I can't see any
usefulness in ever doing that.)
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 7 '07 #5
InuY4sha <ri************ *@gmail.comwrit es:
On 7 Ago, 12:46, Army1987 <army1...@NOSPA M.itwrote:
>On Tue, 07 Aug 2007 10:11:38 +0000, InuY4sha wrote:
Hi,
I hope to be not off topic..
I have a string on a file "00:32:43:54:A2 :2D" (let's say a MAC
address)
I use fscanf like this
fscanf( file, "%02X:%02X:%02X :%02X:%02X:%02X ",
ptr, ptr+1, ptr+2,
ptr3, ptr+4, ptr+5);
It works, but I get warnings as the expected size is int while I'm
storing onto a uint8_t array.. is there a format to store onto a
uint8_t array instead of using a int array? ( I mean to don't get any
warning)
Things are working but I hate that warnings..
<snip>
>Use the macro SCNx8 in <inttypes.hwhic h expands to a string
literal, like this:
fscanf(file, "%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8
":%02" SCNx8 ":%02" SCNx8, ptr, ptr+1, ptr+2, ptr+3,
/* you didn't actually mean ptr3, did you? */, ptr+4, ptr+5);
<snip>
This way I don't get the warning but it doesn't seem to be working
fine as the data retrieved is wrong... (while before it was fine..)
From what I see the printed data is totally random.
I think you'll have to post a short example of this "not working".
Ideally a full program that read the data from stdin. Often, just
trying to generate a short example of the problem, you will find out
what is wrong for yourself.

--
Ben.
Aug 7 '07 #6
InuY4sha <ri************ *@gmail.comwrot e:
I hope to be not off topic..
I'm writing this reply presuming you aren't.
I have a string on a file "00:32:43:54:A2 :2D" (let's say a MAC
address)
I use fscanf like this
fscanf( file, "%02X:%02X:%02X :%02X:%02X:%02X ",
ptr, ptr+1, ptr+2,
ptr3, ptr+4, ptr+5);
It works, but I get warnings as the expected size is int while I'm
storing onto a uint8_t array.. is there a format to store onto a
uint8_t array instead of using a int array? ( I mean to don't get any
warning)
If you're using uint8_t, you're using C99. If you're using C99, you
should also have <inttypes.h>. If you #include <inttypes.h(a t which
point you can ditch <stdint.h>, since that is automatically included by
<inttypes.h>) , you can use SCNx8. For example, instead of

fscanf(file, "%02X...", ptr);

you can use

fscanf(file, "%02" SCNx8 "...", ptr);

and so on.
<inttypes.hal so defines similar macros for other sized integers, for
int_fast<foo>, int_least<bar>, intmax_t, and so on; and for the printf()
family as well as the scanf()s. Less relevantly, there are also a couple
of useful functions for dealing with (u)intmax_t.

Richard
Aug 7 '07 #7

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

Similar topics

3
4403
by: Wei-Chao Hsu | last post by:
There are some data files look like 1.) data1.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 2.) data2.txt
19
3223
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The first position is the info (the product) I want to retreive for the corresponding code. Assuming that the codes are unique for each product and all code data is on one line.
10
25604
by: QQ | last post by:
I am trying to read data from Excel files I use the following codes, however, it didn't work Can anyone tell me what the format excel files have, what I should do to read it from C? Thanks a lot! #include <stdio.h>
29
10439
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and then print out the contents of the 2d array to the screen. I wil also need to append data to the .dat file but mostly right now I'm worrying about getting the info into the 2d array. My .dat file looks like this 1 20000 2 30000 3 40000
4
4234
by: John | last post by:
I need to read data from the file like the following with name and score, but some line may only has name without score: joe 100 amy 80 may Here's my code, but it couldn't read the line with "may" because there is no score. Anyone knows what is the workaround to this problem?
7
3061
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716 2718 2895 I wanna to read the data to an array, as the follows:
5
1372
by: Anthony Smith | last post by:
I have data that is in this format: 8-8-19-29-1-4-41 I wanted to write a simple function or regular expression that verified that data is in that format. The numbers should only be 1 or 2 digits. There should be 6 dashes (-) with 7 numbers around the dashes. Here are examples of bad data: 8-19-29-1-4-41 (Only 6 dashes) 5-1-2-3-4-S-67-4 (Includes a letter) 5-1-2-3-4-222-67-4 (has three digits in one space)
59
5594
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading the + character, and then sending the remainder into fscanf() like
30
1949
by: George | last post by:
1 0001000000000000001 2 0001000000000000001 3 10000011001000000000000001 4 10000011001000000000000001 5 10000011001000000000000001 6 10000011001000000000000001 7 10000011001000000000000001 8 10000011001000000000000001 9 10000011001000000000000001 10 10000011001000000000000001
0
9669
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, 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...
0
10426
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...
0
10207
jinu1996
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...
1
7537
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
6776
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();...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
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
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
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.