Linux Search Pattern Question: Download Search Pattern PDF
Do you know what is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
int main()
{
int fd;
char *buff;
buff = (char *)malloc(sizeof(char)*5);
fd = open("google.txt",O_RDWR|O_CREAT);
write(fd,"Linux",5);
read(fd,buff,5);
printf("%sn",buff);
}
a) it will print nothing
b) it will print the string "Linux"
c) segmentation fault
d) none of the mentioned
Answer:
a) it will print nothing
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 google]# gcc -o san san.c
[root@localhost google]# ls
san san.c
[root@localhost google]# ./san
[root@localhost google]# ls
san san.c google.txt
[root@localhost google]# vim google.txt
[root@localhost google]#
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 google]# gcc -o san san.c
[root@localhost google]# ls
san san.c
[root@localhost google]# ./san
[root@localhost google]# ls
san san.c google.txt
[root@localhost google]# vim google.txt
[root@localhost google]#