Linux Search Pattern Question:
Download Questions 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]#

Download Search Pattern Interview Questions And Answers PDF

Previous QuestionNext Question
Tell me what is the output of this program?

#include<stdio.h>
#include<fcntl.h>

int main()
{
int fd, count;
char ch;
fd = open("google.txt",O_RDWR|O_CREAT);
write(fd,"s",1);
lseek(fd,0,SEEK_SET);
write(fd,"d",1);
lseek(fd,0,0);
read(fd,&ch,1);
printf("%cn",ch);
return 0;
}
a) d
b) s
c) sd
d) none of the mentioned
Tell us what is the output of this program?

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>

int main()
{
int fd, count;
char ch, *buff;
buff = (char *)malloc(sizeof(char)*10);
fd = open("san.c",O_RDONLY);
count = read(fd,buff,5);
printf("%dn",count);
return 0;
}
a) 5
b) 10
c) 0
d) -1