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?

Recommended Answers

All 5 Replies

That's because your writing the binary/hex value of the integer and not its ascii text representation...

The best way to read and write a structure is to write the structure all at once i.e.

struct person 
{

};

struct person Person;

write(fd, (char*)&person, sizeof(struct person));
read(fd, (char*)&person), sizeof(struct person));

Ok here is what I get in the file after writing the structure directly:
Kim
@� ��h�`�T � USA
$������ PriyaRai
������ .N=� India
������ JongHu
o @�X��� China
���.N=�(���� Mwemba
������ @� SouthAfrica
JeanPotiron
France

Here is the code:

#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:" };
 

  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;
    write( x,(char *)&myArr[i], sizeof(struct Person) );
  }
  return 0;
}

Did I do exactly what you said or did I miss something?Or is there some other way out to write a 32 bit integer with write()?

Yeah so what's the problem? Try reading the file back into a structure Person like:

struct Person pread;

for( i = 0; i < 2; i++ )
  {
      read(x, (char*)&pread, sizeof(struct Person));
      fprintf(stdout, "name->%s\n", pread.name);
      fprintf(stdout, "address->%s\n", pread.address);
  }

and you'll find your data's all there and intact

Actually the problem is that my Labsheet for my course says to use only open(),close(),read,write() and lseek() when doing the questions and I was trying to answer a question that says write a program that reads the name and address of a number of persons and computes the length of the name and address and writes all those information to a file.I know how to write the name and address and to find their length but the problem is how to write the integer to file using write(it should display the integer value in the file not the binary/hex representation); is it even possible to do this with those UNIX system calls?

Then you'll have to convert the hex/binary value(s) into the a ascii text representation before you write them to file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.