Hello I'm doing my OS homework and I need to know how to read an write a 32 bit integer with the write system call
Here is the code that I wrote:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
struct Person
{
char name[21];
int ch_in_name;
char address[21];
int ch_in_address;
};
int main()
{
typedef unsigned char *byte_pointer;
int i;
struct Person myArr[5];//An array of struct Person
int x = open( "/home/user/Desktop/Labsheets/Labsheet4/input09.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU );
char buf[] = { "Please enter the person's name:" };
char buf1[] = { "Please enter the person's address:" };
char *ptr;
for( i = 0; i < 5; i++ )
{
write( 1, buf, strlen(buf) );
int a = read( 0, myArr[i].name, 21 );
myArr[i].ch_in_name = a;
write( 1, buf1, strlen(buf1) );
int b = read( 0, myArr[i].address, 21 );
myArr[i].ch_in_address = b;
ptr = (char*)&a;
write( x, ptr, sizeof(int) );
write( x, myArr[i].name, a );
ptr = (char*)&b;
write( x, myArr[i].address, b );
write( x, ptr, sizeof(int) );
}
return 0;
}
And here is what I get in the file input09:
Kim
USA
PriyaRai
India
Jong
China
JeanNezCassez
France
Yaroslav
Russia
well instead of the integer there is some encoding symbol; can someone help me please?