#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?

Dani AI

Generated

Quick summary for (and building on ’s points):

The printed 5 is simply the number of bytes read. lseek moved the file offset to byte 10, and the file contains 15 bytes (the trailing newline counts), so reading up to 10 bytes from offset 10 returns the 5 bytes available (15 − 10 = 5). That value is what printf("%d\n", n) shows.

Why _CIS appears and looks odd: the buffer filled by read() is not automatically NUL-terminated, so using printf("%s") on it is undefined behavior. In practice the five bytes read happen to form the text _CIS (and maybe a newline), and printf prints until it finds a NUL somewhere later in memory. The correct pattern is to terminate the buffer yourself after read():

ssize_t n = read(fd, buf, sizeof(buf) - 1);
if (n > 0) buf[n] = '\0';
printf("%zd\n", n);
printf("%s\n", buf);

What the program does to the file: after the read the file offset is at EOF, so write(fd, buf1, 10) appends CE_and_IST to the file. The later lseek(fd, 0, SEEK_SET) resets the offset to 0; close(1); dup(fd); close(fd); makes file descriptor 1 (stdout) refer to that same open file at offset 0, so write(1, &buf2, 1) overwrites the first byte of the file (changing the leading W to the single character written). That explains why the file ends up with the first character replaced and the appended CE_and_IST still present.

Notes and quick checks:

  • Watch the filename mismatch in the post (myfile vs myprofile). Verify open() succeeds and check its return.
  • Always check return values of open, read, write, lseek.
  • Use ssize_t with %zd for byte counts.
  • Consider pread/pwrite or fread/fwrite for simpler, safer I/O and to avoid juggling global file offsets.

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.