#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>


 int main() {
     char buf[15];
     char *buf1 = "CE_and_IST";
     char buf2 = 'z';
     int fd = open("myfile", O_RDWR);
     lseek(fd, 10, SEEK_SET);
     int n = read(fd, buf, 10);
     printf("%d\n", n);
     n = write(fd, (const void *)buf1, 10);
     printf("%s\n", buf);
     lseek(fd, 0, SEEK_SET);
     close(1); dup(fd); close(fd);
     n = write(1, (const void *)&buf2, 1);
     close(1);
     return 0;
     }

and the contents of "myprofile" is Welcome_to_CIS

so the output for this code is, and I'm really confused, is 5 come from 15 minus 10, I thought the length of the code is 15. like what does exactly this code do?

5
_CIS

and the contents of "myprofile" is after execution is
Zelcome_to_CIS

CE_and_IST

How did they get to this?

Arrays are zero indexed, as you see when you seek to the 0 possition in the file and replace that position with a single 'Z' character. generally I recommend sticking to a specific indetation format with spacing between lines more often.

as for execution:
First it is creating your buffers, then it opens your file and seeks to the position with index 10.

Then it reads as much of the file as it can fit into buf, and stores the number of characters read into n.

Then you seek back to the beginning of the file, and...... ?

Then you write the 'Z' into the file, and again......... ?

Then you return 0, exiting the programn, and the computer fixes the open files and closes the others you opened.

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.