-3

I want to read 'c' and "Hello world!!" but i see just the hexadecimal part.

Here the structure :

typedef struct step
{  

int n;

 char l;

 char c[25];                                                      

 } toto;




    int fd = open("one-structure.yolo", O_CREAT | O_WRONLY, 777);
    toto some;

    some.n = 212347;
    some.l = 'c';
    some.c[25]  = "Hello world!!";
    write(fd, &some.n, sizeof(int));
    write(fd, some.l, sizeof(char));
    write(fd, some.c, 13);

    close(fd);

the result : {=U@�f

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Lu-6
  • 1

2 Answers2

2

Apart from the problem pointed by Sourav Ghosh.

You cannot assign string to array using assignment operator.

some.c[25]  = "Hello world!!";

You need to use strcpy.

   strcpy(some.c, "Hello world!!");

or

make some.c as pointer.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
1

In addition to Kiran answer, you need to change write(fd, some.l, sizeof(char)); to write(fd, &some.l, sizeof(char)); as write needs a pointer.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127