Pete wrote:
I having a problem reading all characters from a file. What I'm
trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I
want to && with a mask 0xFF000000 then >> right shift 24 bits storing in
result then printing the result.
I thing a while or for loop is needed but I'm not quite sure how to
go about it. How do I step through each character in this case and store it
for use and passing to another function.
Any help is greatly appreciated
Pte
#include <std.h>
main(int argc, char *argv[]) {
FILE *ptr;
unsigned int result;
ptr = fopen("file.txt", "r"); /*file.txt contains a single 32 bit
hex value 0x8FB4902F to be && anded*/
result = ptr && 0xFF000000 >> 24;
printf("result = %s", result);
fclose(ptr);
}
Hi,
The file pointer is just a handle to the open file. On some systems
the FILE* 0x01 is also called stdout, 0x02 stdin, and 0x03 stderr, or
perhaps it's 0x01 is stdin and 0x02 is stdout, they're probably defined
in one of the standard header files.
So ptr, which has a type FILE*, is just the file handle. It's not the
contents of the file.
When you included stdio.h, there are a bunch of function prototypes
declared there. Look at stdio.h. The functions that begin with f,
like fgets, fgetc, fread, fwrite, etcetera, take as their first
argument one of those file handles there. Their other arguments might
be a buffer of memory to contain the results of the operation, or they
might have as a return value the results of the function. There are
other functions that don't begin with f, they do pretty much the same
thing as the file functions, except they operate on stdin and stdout,
which are called standard input and standard output, and mean the input
and output from the command line console.
So, you have to read from the file into some other data location than
onto the file pointer, because that would alter the value storing the
open file handle to some meaningless value.
You have to be careful when you read from the file into multi-byte data
types. That's the big deal between little-endian and big-endian
processors, they store multi-byte integer types in opposite order.
For example, if the data file opened in your hex editor or hexadecimal
viewer has:
0x11 0x22 0x33 0x44
then if you load that on a big-endian processor as a 4-byte integer,
then the processor sees it as
11223344
If you load it on a little-endian processor, then it would be
44332211
While that is so, if you declare the number in your C source code file
instead of reading it from a data file,
int x = 0x11223344;
then that is
11223344
on processors of either of those byte orders.
So, you want to read four bytes from the file. Look at fread, you
probably want to use that. One of its arguments is the _address_ of
the memory buffer that you want to contain the data read from the file.
Then the fread (file read) function copies the values from the data
file into memory at that memory location for the processor to load
bytes starting from the memory address onto the processors 32-bit,
4-byte register, according to the byte order. You get the address of a
variable with &.
Then, when you want to print that value, it's a single character you
want to print instead of a string. The string (C string) or char* is
just the memory address of the beginning of a series of those one-byte
character or char values in memory, where the last one is a zero, so
the functions upon them know when to stop.
C is lots of fun, one mistake and it all goes bad.
Ross F.