[英]When I program in Linux using C, I use the open or creat functions and end up behaving differently
//error handle
void my_err(const char* errno_string,int line){
fprintf(stderr,"line:%d ",line);
perror(errno_string);
exit(1);
}
//self-definded read data function
int my_read(int fd){
int len;
int ret;
int i;
char read_buf[64];
//get length of file and keep point of file at the srart
if(lseek(fd,0,SEEK_END) == -1){
my_err("lseek",__LINE__);
}
if((len = lseek(fd,0,SEEK_CUR)) == -1){
my_err("lseek",__LINE__);
}
if(lseek(fd,0,SEEK_SET) == -1){
my_err("lseek",__LINE__);
}
printf("len:%d\n",len);
//read data
if((ret = read(fd,read_buf,len)) < 0){
my_err("read",__LINE__);
}
//print data
for(i = 0;i<len;i++){
printf("%c",read_buf[i]);
}
printf("\n");
return ret;
}
int main()
{
int fd;
char write_buf[32] = "hello boy!";
//create example2 in current directory
if((fd = creat("example2.c",S_IRWXU)) == -1){
// if((fd = open("example2.c",O_RDWR|O_CREAT|O_TRUNC,S_IRWXU)) == -1){
my_err("open",__LINE__);
}else{
printf("craete file success\n");
}
//write data
if(write(fd,write_buf,strlen(write_buf)) != strlen(write_buf)){
my_err("write",__LINE__);
}
my_read(fd);
//Spacing of presentation files
printf("/*------------*/\n");
if(lseek(fd,10,SEEK_END) == -1){
my_err("lseek",__LINE__);
}
if(write(fd,write_buf,strlen(write_buf)) != strlen(write_buf)){
my_err("write",__LINE__);
}
my_read(fd);
close(fd);
return 0;
}
Line 43 is this part of main
第 43 行是 main 的这一部分
//create example2 in current directory
if((fd = creat("example2.c",S_IRWXU)) == -1){
my_err("open",__LINE__);
}else{
printf("craete file success\n");
}
When I use creat , I get an error line:43 read: Bad file descriptor, but I get the correct result with open .当我使用creat时,我得到一个错误 line:43 read: Bad file descriptor, 但是我得到了正确的结果open 。 Shouldn't both functions return file descriptors?
两个函数都不应该返回文件描述符吗? Why should creat return the wrong file descriptor
为什么要创建返回错误的文件描述符
When I use creat , I get an error line:43 read: Bad file descriptor, but I get the correct result with open .当我使用creat时,我得到一个错误 line:43 read: Bad file descriptor, 但是我得到了正确的结果open 。 Shouldn't both functions return file descriptors?
两个函数都不应该返回文件描述符吗? Why should creat return the wrong file descriptor
为什么要创建返回错误的文件描述符
Shouldn't both functions return file descriptors?
两个函数都不应该返回文件描述符吗?
They should and they do.他们应该而且他们确实这样做了。
Why should creat return the wrong file descriptor
为什么要创建返回错误的文件描述符
It shouldn't and it doesn't.它不应该也不应该。
read
fails with Bad file descriptor
error, not creat
. read
失败并出现Bad file descriptor
错误,而不是creat
。
creat
opens file write-only, so you can't read from it. creat
以只写方式打开文件,因此您无法从中读取。 It's a bad file descriptor if you want to read from it.如果您想从中读取,这是一个错误的文件描述符。
The call creat(path, mode)
behaves the same as the call open(path, O_CREAT | O_WRONLY | O_TRUNC, mode)
.调用
creat(path, mode)
的行为与调用open(path, O_CREAT | O_WRONLY | O_TRUNC, mode)
。 On success, the file is opened for writing only.成功后,打开文件仅用于写入。 The file descriptor passed to
read
needs to be open for either reading and writing or for reading only.传递给
read
的文件描述符需要为读写或只读打开。 If the file descriptor is open for writing only, calls to read
with that file descriptor will fail.如果文件描述符只为写入而打开,则使用该文件描述符
read
的调用将失败。 When read
fails, the error number EBADF
means that the file descriptor is not a valid file descriptor open for reading.当
read
失败时,错误号EBADF
表示文件描述符不是打开以供读取的有效文件描述符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.