Reading hex values from a text file - what am I doing wrong?

This is a discussion on Reading hex values from a text file - what am I doing wrong? within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm trying to read MAC addresses from the following text file: Code: 192.168.1.1 00:0c:41:ae:5a:04 192.168.1.103 00:16:76:1d:4d:a9 192.168.90.130 00:0C:29:F2:25:63 ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    34

    Reading hex values from a text file - what am I doing wrong?

    Hey guys,

    I'm trying to read MAC addresses from the following text file:
    Code:
    192.168.1.1	00:0c:41:ae:5a:04
    192.168.1.103	00:16:76:1d:4d:a9
    192.168.90.130	00:0C:29:F2:25:63
    192.168.90.131	00:0C:29:C8:D3:9E
    192.168.1.104	00:04:75:D5:44:2C
    using the following code:
    Code:
    fscanf(database, "%[^\t]\t%02x:%02x:%02x:%02x:%02x:%02x\n", ip_addr, &eth_addr[0], &eth_addr[1], &eth_addr[2], &eth_addr[3], &eth_addr[4], &eth_addr[5])
    However, I'm getting all zeros in the eth_addr array. What am I doing wrong?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    How is eth_addr declared?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    Quote Originally Posted by Prelude
    How is eth_addr declared?
    It is an array of unsigned chars.

    Okay, now I'm so desparate I tried the simplest program possible:
    Code:
    #include <stdio.h>
    
    int main(void) {
    	FILE *f = fopen("test.txt", "r");
    	unsigned char test0, test1;
    	fscanf(f, "%02x:%02x", &test0, &test1);
    	printf("%d %d\n", test0, test1);	
    	return (0);
    }
    and the test.txt file contains
    Code:
    45:68
    The output of the program is "0 104"! I'm totally confused now. By the way, I'm using the gcc compiler on Ubuntu Linux.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It is an array of unsigned chars.
    That's your problem then. scanf expects certain types when you use a format modifier, and %x expects an unsigned int. Failure to meet scanf's expectations will result in wierd things.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    Quote Originally Posted by Prelude
    >That's your problem then. scanf expects certain types when you use a format modifier, and %x expects an unsigned int. Failure to meet scanf's expectations will result in wierd things.
    Ah, I get it. Thanks a lot!

  6. #6
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    but then , how is his sample program working? He claims he is getting a non-zero output inspite of declaring test0 and test1 as unsigned chars?
    In the middle of difficulty, lies opportunity

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how is his sample program working?
    The result is undefined, so it could be anything.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 05:38 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  3. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-31-2009, 12:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21