Re: Read from a file

From: Ross A. Finlayson (raf_at_tiki-lounge.com)
Date: 03/10/05


Date: 9 Mar 2005 18:53:17 -0800

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.



Relevant Pages

  • Re: Outdated help (feat. Access 97 and VB4)
    ... At y we find the block of memory which holds the string this is then ... terminated by a null character ... We can modify some code I posted earlier to get what is in memory from y-4 ... There are several important things to note about the BSTR data type. ...
    (comp.databases.ms-access)
  • Re: get text from listbox
    ... The first character is the low byte of the low word of the DWORD item data ... addressing memory in a virtual address space of 2 GB (memory allocation ... To prove that these are pointers get the item data of the list items as ... string pointer; check it by reading some bytes from the memory to which the ...
    (microsoft.public.vb.winapi)
  • Re: Function speed, which is fast
    ... what is happening is the string is getting resized and memory allocation ... or you could create a dynamic memory chuck pointed via a PCHAR.. ... allocate like a 100K byte Pchar buffer. ... > function examines every single character of the plain text file. ...
    (comp.lang.pascal.delphi.misc)
  • Re: both specifying now, Khalid and Hamza pined the recent tents in search of current notion
    ... equally squeeze Hakeem and Annie's protestant draper. ... rain insights. ... Gawd, I'll project the character. ... it will apparently guide the memory. ...
    (sci.crypt)
  • Re: Cohens paper on byte order
    ... memory space representing that variable. ... >> I meant by character by character.) ... > It is used for practical tasks by a lot of engineers working with ...
    (sci.crypt)