简体   繁体   English

当我使用 C 在 Linux 中编程时,我使用 open 或 creat 函数并最终表现不同

[英]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.

相关问题 Mac OSX和Linux上C程序的行为有所不同 - C program behaving differently on Mac OSX and Linux 当使用 open 命令和 O_CREAT 时,它会附加一个“?” 到文件名的末尾,如何删除它? - When using the open command, and O_CREAT, it is appending a "?" to the end of the file name, how do I remove this? 使用O_CREAT时执行open - The execution of open when using O_CREAT 如何在基于Linux的系统上的c程序中使用mqueue? - How do I use mqueue in a c program on a Linux based system? 为什么我的C程序根据执行的目录表现不同? - Why is my C program behaving differently based on the directory it is executed from? 使用“删除”功能时程序挂断 - Program gets hung up when I use “delete” function 由于某些原因,当我针对Oracle GSS进行链接时,其行为开始有所不同(Solaris) - For some reason when I link against Oracle GSS starts behaving differently (Solaris) 如何获得Linux下C ++程序中调用的函数的打印? - How do I get a print of the functions called in a C++ program under Linux? 在ubuntu linux端使用套接字的客户端的C程序未运行 - C program for client using socket on ubuntu linux end not running 为什么为C ++编译时realloc()神秘地表现出不同? - Why is realloc() mysteriously behaving differently when compiled for C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM