What is the output of this program? [code] #include<stdio.h> #include<stdlib.h> #include<fcntl.h> int main() { int

What is the output of this program?
Code:
   #include<stdio.h>
   #include<stdlib.h>
   #include<fcntl.h>
   int main()
   {
       int fd;
       char *buff;
       buff = (char *)malloc(sizeof(char)*5);
       fd = open("123hoidap.txt",O_RDWR|O_CREAT); 
       write(fd,"Linux",5);
       read(fd,buff,5);
       printf("%s\n",buff);
   }
A. it will print nothing
B. it will print the string “Linux”
C. segmentation fault
D. none of the mentioned
 
Answer: a
Explanation: We have to use “lseek” system call if we want to read the file from the beginning just after writing into it.
Output:
[root@localhost 123hoidap]# gcc -o san san.c
[root@localhost 123hoidap]# ls
san san.c
[root@localhost 123hoidap]# ./san
[root@localhost 123hoidap]# ls
san san.c 123hoidap.txt
[root@localhost 123hoidap]# vim 123hoidap.txt
[root@localhost 123hoidap]#
 

Tìm kiếm

Back
Top