Well, your code works okay on my end with the sample file you gave. It's difficult to troubleshoot something you can't reproduce.
Printable View
Well, your code works okay on my end with the sample file you gave. It's difficult to troubleshoot something you can't reproduce.
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.
the very first thing int he file is a '3' no spaces before or anything. so it should be a valid hex character?
>I'm not sure if scanf actually reads and advances past the whitespace?
Except for %c, %[, and %n, leading whitespace is skipped.
Works for me,
using this input file:
And this code:Code:1C 3
A4 7
23 C8
1F 98
and I get this output: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,"%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);
}
ToddCode:[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.
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
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!
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
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
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.
I didn't find any mention of "t" in my copy of a C99 draft. What reference are you using?
Stick fopen() into google.
- http://www.cplusplus.com/reference/c...dio/fopen.html
- http://opengroup.org/onlinepubs/0079...xsh/fopen.html
- http://www.cppreference.com/stdio/fopen.html
- http://man.he.net/man3/fopen
- http://www.opengroup.org/onlinepubs/...ons/fopen.html
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.
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'?Quote:
It's part of the C89 standard. Most of those references say so. (C99, too, if the last link is to be believed.)
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.