早教吧 育儿知识 作业答案 考试题库 百科 知识分享

C语言的一道题目?#include#include#include#includeintmain(void){chartest;intfd;if((fd=open("test.dat",ORDONLY))==-1){perror("Cannotopenthetest.datfile");return1;}if(fork()==-1){perror("Cannotcreatethechildproces

题目详情
C语言的一道题目?#include #include #include #include int main(void){ char test; int fd; if((fd=open("test.dat",O_RDONLY))==-1){ perror("Cannot open the test.dat file"); return 1; } if(fork()==-1){ perror("Cannot create the child process"); return 1; } read(fd,&test,1); printf("Process ID: %ld read the character: %c\n",(long)getpid(),test); close(fd); return 0; } 如这个程序所示,其中有几个以为,能否帮忙解释下,第一个if中的return 1是什么意思,open()函数返回的是什么?还有第二个中的干嘛要用fork函数,这其中需要创建进程吗,还有第二个的return 1 的含义。 接下来是最后第四行的read函数,&test代表什么?1代表什么?
▼优质解答
答案和解析
return 是针对你的主函数 为int型,所以必须返回int型返回值。 read(fd,&test,1);read的三个参数是:文件描述符:fd;缓冲区指针:buffer;缓冲区大小:sizeof(buffer) fd是文件句柄,&text指向保存读出的内容的空间,1是大小。 对于open返回值判断,返回值与-1相比较,相等,该地址不存在,不相等则继续操作。 若有希望你请追问