How to use read() system call to read an 32bit integer

Y

Yin Zhu

Hello, All

Maybe this question is not proper to post here:( but anyway would
someone help me?

I am doing my operating system homework. I know how to use a buffer
string to do read and write.

But how about 32-bit int?

Here is my method to do:

typedef unsigned char *byte_pointer;
write(fd, (byte_pointer)&i, sizeof(int));
read(fd, (byte_pointer)&j, sizeof(int));

it works.

I don't know whether my method is a common way. or there's some other
ways to do it.


thanks in advance.
 
K

Kenny McCormack

Hello, All

Maybe this question is not proper to post here:( but anyway would
someone help me?

I am doing my operating system homework. I know how to use a buffer
string to do read and write.

But how about 32-bit int?

Here is my method to do:

typedef unsigned char *byte_pointer;
write(fd, (byte_pointer)&i, sizeof(int));
read(fd, (byte_pointer)&j, sizeof(int));

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

--
Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language
 
J

jacob navia

Yin said:
Hello, All

Maybe this question is not proper to post here:( but anyway would
someone help me?

I am doing my operating system homework. I know how to use a buffer
string to do read and write.

But how about 32-bit int?

Here is my method to do:

typedef unsigned char *byte_pointer;
write(fd, (byte_pointer)&i, sizeof(int));
read(fd, (byte_pointer)&j, sizeof(int));

it works.

I don't know whether my method is a common way. or there's some other
ways to do it.


thanks in advance.

In standard C you would use fread and fwrite instead of read and write.
But please do not forget:

If it works, it works. Do not fix something that's not broken
 
Y

Yin Zhu

In standard C you would use fread and fwrite instead of read and write.
But please do not forget:

If it works, it works. Do not fix something that's not broken


but I need to compare fread with read.
fread has its own buffering mechanism, I want to write a buffered read
to compare with fread.
 
C

Christopher Benson-Manica

[comp.lang.c] jacob navia said:
Yin Zhu wrote:
Maybe this question is not proper to post here:( but anyway would
someone help me?

comp.unix.programmer would have been a good place.
If it works, it works. Do not fix something that's not broken

Just because something works doesn't mean it isn't broken.
 
H

Hallvard B Furuseth

And open the file with "b" in the mode string, since it's presumably a
binary file.
 
J

jacob navia

Christopher said:
[comp.lang.c] jacob navia said:
Yin Zhu wrote:
Maybe this question is not proper to post here:( but anyway would
someone help me?

comp.unix.programmer would have been a good place.
If it works, it works. Do not fix something that's not broken

Just because something works doesn't mean it isn't broken.

And what's wrong with that call to write or read?
Outside the fact that is not ANSI (as I indicated to the OP) there is
nothing wrong with it!
 
C

Christopher Benson-Manica

[comp.lang.c] jacob navia said:
And what's wrong with that call to write or read?
Outside the fact that is not ANSI (as I indicated to the OP) there is
nothing wrong with it!

It's possible that the program was required to be written in standard
C (we don't know), in which case it IS broken since it fails to meet
the requirement.
 
W

Willem

Yin wrote:
) Hello, All
)
) Maybe this question is not proper to post here:( but anyway would
) someone help me?
)
) I am doing my operating system homework. I know how to use a buffer
) string to do read and write.
)
) But how about 32-bit int?
)
) Here is my method to do:
)
) typedef unsigned char *byte_pointer;
) write(fd, (byte_pointer)&i, sizeof(int));
) read(fd, (byte_pointer)&j, sizeof(int));

What's wrong with:

write(fd, &i, sizeof(i));
read(fd, &i, sizeof(i));

?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
T

Tor Rustad

Yin said:
I am doing my operating system homework. I know how to use a buffer
string to do read and write.

Huh.. is this really OS homework?!

But how about 32-bit int?

Here is my method to do:

typedef unsigned char *byte_pointer;
write(fd, (byte_pointer)&i, sizeof(int));
read(fd, (byte_pointer)&j, sizeof(int));

it works.

It shouldn't, unless your code runs in *user* space. write() and read()
are POSIX system calls, but in *kernel* space those interfaces would be
different.

I don't know whether my method is a common way. or there's some other
ways to do it.

Hint: try to store the int, such that the result doesn't depend on int
byte order (big endian & little endian).
 
K

Kenny McCormack

why does he say that so often ? is he a bot ???

I say it far less often than, e.g., Keith Thompson, says it.

And I say it a much more entertaining way, to boot.
 
S

Stan Milam

Yin said:
Hello, All

Maybe this question is not proper to post here:( but anyway would
someone help me?

I am doing my operating system homework. I know how to use a buffer
string to do read and write.

But how about 32-bit int?

Here is my method to do:

typedef unsigned char *byte_pointer;
write(fd, (byte_pointer)&i, sizeof(int));
read(fd, (byte_pointer)&j, sizeof(int));

it works.

I don't know whether my method is a common way. or there's some other
ways to do it.


thanks in advance.

Just one thing: with some systems an int is 64 bits. So, I would
include <stdint.h> and use sizeof(int32_t).

Regards,
Stan Milam
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top