reading in a text file containing hex values

This is a discussion on reading in a text file containing hex values within the C Programming forums, part of the General Programming Boards category; hi all, thankyou once agian for your help the other day, but once agian i have been banging my head ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    16

    reading in a text file containing hex values

    hi all, thankyou once agian for your help the other day, but once agian i have been banging my head against a brick wall and need to call on your wisdom.

    I have a text file containing hex values of the form

    12 3
    24 7
    23 78
    10 98

    (maxium of 2 hex values per colum, 8 bits but sometimes there is only one!)

    where the first colum contains an address and the second a data value to be written to that address. I need to read the file into an array of the form

    address[2014]
    data[2014]

    as their are 2014 lines of code.

    my attempt so far is :-

    Code:
    #include <stdio.h>
    
    
    void display(int a, int v)  ;
    
    int main()
    
    {
    
    	FILE *file_ptr;
    	int address[2014], value[2014], i, j;
    	
    	file_ptr = fopen("C:\\Documents and Settings\\Owner\\Desktop\\sta013.txt","rt");
    	if (file_ptr !=NULL)
    	{
    		printf("settings file found\n");
    		
    		
    		i=0;
    		while (i<1024)
    			{
    				i++;
    				fscanf(file_ptr,"%0x",&address[i]);
    				fscanf(file_ptr,"%0x",&value[i]);
    			}
    			
    		printf("total number of values read %d\n",i);
    		for (j=0; j<i-1;j++)
    			{
    			
    			
    			display(address[j],value[j]);
    			
    			
    			
    			
    			}
    		fclose(file_ptr);
    		return 0;
    	}
    	else
    		printf("file not found\n");
    		return 0;
    }
    
    
    void display(int a,int v)
    {
    	printf("%x	%x\n", a,v);
    
    }

    however this gives the following results

    Code:
    settings file found
    total number of values read 2014
    e7ff0010	                e7ff0010
    e800e800	e800e800
    e7ff0010	                e7ff0010
    e800e800	e800e800
    etc ...                       etc...
    would really appreciate any help! this little problem is stopping my completing vital code for my thesis and i am really stressed. Unfortunately code is not my strongest point so i may have dont something silly. but please point it out.

    Thankyou all.

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    307

    Thumbs up

    Code:
    i=0;
    while (i<1024)
    {
    	i++;
    	fscanf(file_ptr,"%0x",&address[i]);
    	fscanf(file_ptr,"%0x",&value[i]);
    }
    You're indexing is off by one. Assuming you want to fill address[0..n] and value[0..n] try this:

    Code:
    i=0;
    while (i<1024)
    {
    	fscanf(file_ptr,"%0x",&address[i]);
    	fscanf(file_ptr,"%0x",&value[i]);
                    i++;
    }
    GCC on Fedora Core 10

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I'm pretty sure %0x means "read a hexadecimal value, but for heaven's sake don't read more than 0 characters". Does it work without the 0?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i++;
    Are you sure you want to skip the first element of the array?

    >fscanf(file_ptr,"&#37;0x",&address[i]);
    >fscanf(file_ptr,"%0x",&value[i]);
    This should be %x rather than %0x.
    My best code is written with the delete key.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    You might also note that this is essentially a for loop:

    Code:
    i=0;
    while (i<1024)
    {
    	fscanf(file_ptr,"&#37;0x",&address[i]);
    	fscanf(file_ptr,"%0x",&value[i]);
    	i++;
    }
    Code:
    for (i = 0; i < 1024; i++)
    {
    	fscanf(file_ptr,"%0x",&address[i]);
    	fscanf(file_ptr,"%0x",&value[i]);
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    Hi, thanks for quick reply.

    have removed the 0 from &#37;0x, aslo have changed the location of i++; but still getting erounus output.

    Code:
    settings file found
    total number of values read 2014
    e7ff0010	e7ff0010
    e800e800	e800e800
    e7ff0010	e7ff0010
    e800e800	e800e800
    any further ideas? thankyou

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Can you define the input file better please, in hex? like this:

    1st pair: 0x01234568 0x0000007
    2nd pair: 0x00123434 0x0000008

    etc, as this format illustrates how many bytes are used by each field. Once we know the file format, we can direct you on how to structure your code.

    Also, I suggest you open the file in binary mode, not text mode.

    Todd

    EDIT - nevermind - you already said you have a TEXT file.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Finally, you may want to decide whether you are reading 2014 or 1024 values from the file.

    Also, if you know how many values there are, why are you doing this:
    Code:
    for (j=0; j<i-1;j++)
    Which, by the way, will end up with one value less than the number in i - so if you read 1024 values, you will print only the first 1023 of them.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Check fscanf for success. It returns the number of items converted, so if both calls don't return 1, you've got an invalid file format.
    My best code is written with the delete key.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Check whether fscanf succeeds:

    Code:
    	line_count = fscanf(file_ptr,"&#37;x",&address[i]);
    	line_count += fscanf(file_ptr,"%x",&value[i]);
    	printf("Did I get two numbers this time? %s\n", line_count==2?"Yes":"No");
    Note that once you get a non-hex value wedged into the input, all the remaining attempts at reading will fail on the same non-hex value -- so maybe check your input file too.
    Last edited by tabstop; 02-25-2008 at 02:12 PM. Reason: copied wrong code

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    Also, I suggest you open the file in binary mode, not text mode.

    Todd
    Quote Originally Posted by Original post
    I have a text file containing hex values of the form...
    Doesn't it make more sense to open it as a text-file if it's a text file? Opening files in binary mode should be the correct thing if the file is a non-text file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Hohoho, my aren't we active today?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    It is 2014 values i need to read, had been playing with a different file hecne the typo in the code sorry.

    The file is of the following format:-

    Code:
    3A	1
    2A	4
    28	0
    29	0
    20	0
    21	0
    22	0
    23	0
    24	0
    25	0
    26	0
    27	8F
    so its 2 hex digits, 8 bits in each colum as a maxium. no row is empty.

    cheers

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    hi,

    tabstop and prelude, have tried your method. unfortunately the answer is NO each time,

    thanks for the replies everyone

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Hm. Well, that means that the very first thing in the file can't be converted to %x format, so that should be easy to find.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 06:13 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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