Read from a file

P

Pete

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);
}
 
R

Ross A. Finlayson

Pete said:
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.
 
M

Mac

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>

You mean said:
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);
}

Hmmm. Don't you have a C book or something? It would really be in your
best interest to read a good book on C.

You might want to browse the FAQ list for this newsgroup. You can find it
at:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/top.html

If that line breaks, you'll have to manually reconstruct it. It's a damn
long URL!

A problem you need to come to terms with right away is that when you write
binary data to file, you greatly sacrifice portability. Also, different
architectures write data to file with different byte ordering.

If you are hell bent on writing binary data to file, you should think in
terms of which bytes you want to get, starting from byte 0 and going up.

For example, in the scenario you show above, in some architectures, the
byte you want might be byte 0, and in others it might be byte 3. I'm
totally ignoring the architectures where sizeof unsigned char == sizeof
long. If you have to do file IO on one of those systems, you are really in
over your head.

Anyway, to make my example more concrete, you could just read in your
bytes one at a time, and do whatever you want with them. When you write
the bytes to file, write them one byte at a time in either little- or
big-endian format (whichever you prefer), but do it explicitly, one byte
at a time.

Here is a program for you to look at and play with:

#include <stdio.h>

int main(void)
{
int c;
FILE *infile;
unsigned long i = 0;

infile = fopen("file.txt", "rb");
if (infile == 0)
{ printf("Error opening file.txt. Quitting.\n");
return 0;
}
while ((c = getc(infile)) != EOF)
{ printf("%08lx: %02x\n", i, (unsigned int) c);
i++;
}
return 0;
}
 
K

Keith Thompson

Ross A. Finlayson said:
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.

You're thinking of 0 for stdin, 1 for stdout, and 2 for stderr
(expression the numbers in hex isn't particularly helpful).

Those small integer values are "file descriptors", which aren't
standard C. The associated file handles are called stdin, stdout, and
stderr; these are of type FILE*, and they are standard C.

File descriptors are used with the non-standard open() and associated
functions; FILE* is used with the standard fopen() and associated
functions.

(When I say "non-standard", I mean that they aren't defined by the C
standard; they are defined by other standards, particularly POSIX.)
 
R

Ross A. Finlayson

Keith said:
You're thinking of 0 for stdin, 1 for stdout, and 2 for stderr
(expression the numbers in hex isn't particularly helpful).

Those small integer values are "file descriptors", which aren't
standard C. The associated file handles are called stdin, stdout, and
stderr; these are of type FILE*, and they are standard C.

File descriptors are used with the non-standard open() and associated
functions; FILE* is used with the standard fopen() and associated
functions.

(When I say "non-standard", I mean that they aren't defined by the C
standard; they are defined by other standards, particularly POSIX.)
San Diego Supercomputer Center <*>
We must do something. This is something. Therefore, we must do
this.

Right!

Thank you for correcting my error(s).

With open(), instead of using "r" or "w" you use constants like
O_RDONLY, O_WRONLY, or O_RDWR, that are or'd together (eg O_WRONLY |
O_CREAT | O_TRUNC to always write new file contents, or O_WRONLY |
O_APPEND to append to the file).

FILE* ptr = fopen("filename", "r");
int fd = open("filename", O_RDONLY, 0744);

Also you can pass to the open() function what is called an octal
permissions mask, or mode_t, where octal means in base 8, octal
constants start with a zero, for example 0744 is an octal number, not
decimal 744, representing the user/group/world access permissions on
the file for read/write/execution. The non-standard POSIX file
functions, which are quite standard POSIX for Portable Open Systems
Interoperable uniX, or actually Portable Operating System Interface,
also have options for locking the file so other processes do not open
it at the same time.

On Windows, the CreateFile() function returns what is called a HANDLE,
where all the types are CAPITALIZED, which is an integer, that
basically has the same options as the POSIX open() or creat()
functions. So, it can be convenient to define a function that just
calls the POSIX open function with the right options to return a file
descriptor, because if you want to use the file functions on other
platforms they're implemented in very much the same way.




The standard file functions are the most regularly available file
functions for C. Use them.


Thanks again, that was a dumb error on my part, because I've known that
for some years and spaced it, and ignored my own advice to look at
stdio.h.


Ross F.
 
R

Richard Tobin

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);

You seem to have forgotten to read anything from the file. Instead you are
doing arithmetic on the FILE * pointer!

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top