Usage of Unix/Linux Low-Level Io functions read & write
Simply read an object:
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
Int main (INT argc, char ** argv)
{
Int FD = 0;
Int pid = 0;
Char buffer [20] = {'/0 '};
Char * read_buffer [20] = {'/0 '};
// FD = open ("/dev/Hello", o_rdwr | o_creat | o_trunc );
FD = open ("/dev/Hello", o_rdonly); // | o_nonblock );
Printf ("FD = % d/N", FD );
If (FD <0 ){
Perror ("/dev/Hello ");
Return-1;
}
Read (FD, read_buffer, sizeof (read_buffer)-1 );
Printf ("read_buffer = % s/n", read_buffer );
Close (FD );
Return 0;
}
---
Below is a simple file,
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
Int main (INT argc, char ** argv)
{
Int FD = 0;
Int pid = 0;
Char buffer [20] = {'/0 '};
Char write_buffer [20] = {'/0 '};
Strcpy (write_buffer, "zhanglinbao ");
FD = open ("/dev/Hello", o_rdwr | o_creat | o_trunc );
// FD = open ("/dev/Hello", o_rdonly );
Printf ("FD = % d/N", FD );
If (FD <0 ){
Perror ("/dev/Hello ");
Return-1;
}
Write (FD, write_buffer, sizeof (write_buffer)-1 );
Close (FD );
Return 0;
}