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; Well, your code works okay on my end with the sample file you gave. It's difficult to troubleshoot something you ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, your code works okay on my end with the sample file you gave. It's difficult to troubleshoot something you can't reproduce.
    My best code is written with the delete key.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    I'm not sure if scanf actually reads and advances past the whitespace? I'm not very familiar with it.
    Other suggestions might include... when you open the file, specify "r", not "rt". And no magic numbers... You can just use sizeof(buffer) instead.
    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.

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    the very first thing int he file is a '3' no spaces before or anything. so it should be a valid hex character?

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm not sure if scanf actually reads and advances past the whitespace?
    Except for %c, %[, and %n, leading whitespace is skipped.
    My best code is written with the delete key.

  5. #20
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Works for me,
    using this input file:
    Code:
    1C 3
    A4 7
    23 C8
    1F 98
    And this code:
    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("/Users/toddburch/Documents/hexdata.txt","rt");
    
    	if (file_ptr == NULL) 
    	{
    		printf("file cannot be opened or does not exist.\n");
    		return -1 ;
    	} 
    	
    	printf("settings file found\n");
    	
    	for (i = 0 ; i < sizeof(address) ; i++ )
    	{
    		fscanf(file_ptr,"&#37;0x",&address[i]);
    		if (feof(file_ptr)) break ; 
    		
    		fscanf(file_ptr,"%0x",&value[i]);
    		if (feof(file_ptr)) break ; 
    	}
    			
    	printf("total number of values read %d\n",i);
    	
    	for (j=0 ; j<i ; j++ ) 
    	{
    		display( address[j], value[j] );
    	}
    
    	fclose(file_ptr);
    	return 0;
    }
    
    void display(int a,int v)
    {
    	printf("%x	%x\n", a,v);
    }
    and I get this output:
    Code:
    [Session started at 2008-02-25 15:26:53 -0600.]
    settings file found
    total number of values read 4
    1c	3
    a4	7
    23	c8
    1f	98
    
    exercise26 has exited with status 0.
    Todd
    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!

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    ok im wondering if its jst my compiler now, as this is part of a larger system i have got to use metrowerks codewarrior. I wonder if their is an issue with that. thanks all for your help. i will post if i get a solution

  7. #22
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    Ok, The good news is i have compiled and ran the code on Microsft visual express edition and it works fine. The bad news is that i need to use this code within the 'metrowerks codewarrior for arm development suite v1.2' as its the software provided by the university and is required to program the arm 7t with the rest of my code. SOOOO any ideas???

    Thankyou!

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That could be a problem in the C library. Perhaps you can read the file with fgets(), and then use strtol() to get the hex numbers out?

    --
    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. #24
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    ok guys time to put this one to bed, I have found a 'workaround ' not a solution.
    After playing with excel i have split the file into two seperate text files one for each column.
    Read them in seperately using duplicated versions of the previous code and hey presto it works.

    Im not sure why codewarrior wouldnt allow the other method, perhaps as matsp said it could be a library issue. that will have to be solved another day after this damned thesis is handed in lol. Thankyou all for you help

  10. #25
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    Other suggestions might include... when you open the file, specify "r", not "rt".
    Yes, "rt" is a Microsoft extension, and is undefined behavior in the standard.

  11. #26
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    No, "rt" is perfectly standard. The standard says you can have one of "r", "w", and "a", followed by an optional "+", followed by an optional "t" or "b". (The "+" can also go after the "t" or "b").

    The "t" in "rt" means "text file" -- and only DOS/Windows systems (AFAIK) differentiate between binary files and text files (which have line ending conversion). Perhaps that's what you were thinking of.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I didn't find any mention of "t" in my copy of a C99 draft. What reference are you using?

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Stick fopen() into google.

    It's part of the C89 standard. Most of those references say so. (C99, too, if the last link is to be believed.)

    I think you'd be hard-pressed to find a reference that says it's not part of the standard . . . which is different, mind, than not mentioning it at all.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    It's part of the C89 standard. Most of those references say so. (C99, too, if the last link is to be believed.)
    My copy of C99 (the final version, not the draft) does not mention 't' as part of the mode argument. As far as I can tell, all the pages you linked to do not mention 't' as part of the mode argument. Are you sure you are not confusing '+' and 't'?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I always thought it wasn't part of the standard, and was just used to avoid confusion by explicitly stating you've opened it in textmode.

Page 2 of 3 FirstFirst 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