473,385 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Read from a file

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);
}
Nov 14 '05 #1
5 2900
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.

Nov 14 '05 #2
Mac
On Thu, 10 Mar 2005 13:01:30 +1100, 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>
You mean <stdio.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);
}


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/Prog...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;
}
Nov 14 '05 #3
"Ross A. Finlayson" <ra*@tiki-lounge.com> writes:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Keith Thompson wrote:
"Ross A. Finlayson" <ra*@tiki-lounge.com> writes:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> 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.

Nov 14 '05 #5
In article <42********@news.iprimus.com.au>, Pete <No****@blank.com> wrote:
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
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.