linux中open函数的问题

linux中open函数的问题,第1张

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdlib.h> 

int main(void)

{

    int     fd = -1 

    if (-1 == (fd = open("/tmp/new", O_CREAT | S_IRWXU))) {

/* 是O_CREAT | S_IRWXU */        perror("open file error")

        exit(1)

    }   

    close(fd) 

    return 0

}

你看看,在tmp目录下有没有new文件

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include <errno.h>

#define BUFFER_SIZE 1024

int main(int argc,char *argv[])

{

int from_fd,to_fd

int bytes_read,bytes_write

char buffer[BUFFER_SIZE]

char *ptr

if(argc!=3)

{

fprintf(stderr,"usage:%s fromfile tofile\n",argv[0])

exit(1)

}

if(from_fd=open(argv[1],O_RDONLY|O_NONBLOCK)==-1)

{

fprintf(stderr,"open %s error!\n",argv[1])

exit(1)

}

memset(buffer, 0x00,sizeof(buffer))

printf("read_start....\n")

bytes_read=read(from_fd,buffer,BUFFER_SIZE)

printf("bytes_read=[%d],buffer=[%s]\n", bytes_read,buffer)

}

这个是3.c文件,编译 gcc -o 33 3.c;

执行 ./33 a.c(其中a.c中的内容是'hello world');

但是程序执行到最后的时候就阻塞掉了,不知道为啥? 各位大虾帮帮忙if(argc!=3)

{

fprintf(stderr,"usage:%s fromfile tofile\n",argv[0])

exit(1)

}

多次打开同一文件与O_APPEND:

1、在linux系统下,一个进程中两次打开同一个文件,然后分别读取,这里可以猜想一下它最终的会出现什么情况?我的猜想是----第一种是fd1和fd2分别读(这个意思是对同一文件读 *** 作的时候,文件描述符fd1和fd2分别从一个文件里面读取到相同的内容);第二张是接续读(这个意思是,文件描述符fd1和fd2,在文件描述符fd1读取一个位置,也就是文件指针指向的一个位置,第二个文件描述的文件指针会接着上次文件指针指向的位置继续往后面移动,通俗的讲,就是读取到的文件内容不一样


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7309275.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存