Linux程序设计——文件操作(底层文件访问)

Linux底层文件访问主要包括以下几个方面的系统调用:

1、write系统调用

#include <unistd.h> 
size_t write(int fildes, const void *buf, size_t nbytes); 

功能:把缓冲区buf的前nbytes个字节写入与文件描述符fildes关联的文件中。 

返回值:实际写入的字节数。

#include <unistd.h>
#include <stdlib.h>
int main()
{
    if((write(1, "Here is some datan", 18)) != 18)
    {
        write(2, "A write error has occurred on file descriptor 1n", 46);
    }
    return 1;
}


2、read系统调用

#include <unistd.h>
size_t read(int fildes, void *buf, size_t nbytes) 

功能:从与文件描述符fildes相关联的文件里读入nbytes个字节的数据,并将它们放到数据区buf中。

返回值:实际读入的字节数。

#include <unistd.h>
#include <stdlib.h>

int main()
{
	char buffer[128];
	int nread;

	nread = read(1, buffer, 128);
	if(nread == -1)
		write(2, "A read error has occurredn", 26);

	if((write(1, buffer, nread)) != nread)
		write(2, "A write error has occurredn", 27);

	return 0;
}


3、open系统调用

#include <fcntl.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
int open(const char *path, int oflags); 
int open(const char *path, int oflags, mode_t mode); 

功能:创建一个新的文件描述符。 

返回值:返回文件描述符。失败返回-1.

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
	int fildes;
	int nread;
	char buf[500];

	fildes = open("/home/furney/Desktop/simple_write.c", O_RDONLY);

	if(fildes != -1)
	{
		nread = read(fildes, buf, 500);
		if(nread == -1)
			write(2, "A read error has occurredn", 26);

		if((write(1, buf, nread)) != nread)
			write(2, "A write error has occurredn", 27);
	}

	return 0;
}


4、umask系统调用

umask是一个系统变量。当文件被创建时,为文件的访问权限设定一个掩码。由三个八进制数字组成,每个数字都是八进制值1、2、4的OR操作结果。


5、close系统调用

#include <unistd.h>
int close(int fildes);
功能:终止文件描述符fildes与其对应文件之间的关联。

返回值:成功是返回0,失败时返回-1。


6、ioctl系统调用

#include <unistd.h>
int ioctl(int fildes, int cmd, ...);
功能:对描述符fildes引用的对象执行cmd参数中给出的操作。


7、lseek系统调用

#include <unistd.h>
#include <sys/types.h>
off_t lseek(int fildes, off_t offset, int whence);
功能:对文件描述符fildes的读写指针进行设置。

返回值:返回从文件头到文件指针被设置处的字节偏移值,失败时返回-1。

8、fstat、stat、lstat系统调用

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
功能:返回与打开的文件描述符相关的文件的状态信息。其中lstat返回的是符号链接本身的信息。


9、dup和dup2系统调用

#include <unistd.h>
int dup(int fildes);
int dup2(int fildes, int fildes2);
功能:dup复制文件描述符。dup2通过明确指定目标描述把一个文件描述符复制为另一个。


下面的示例程序是利用以上系统调用实现文件复制,每次从源文件中读取数据块复制到目标文件。

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
 
int main()
{
    char block[1024];
    int in, out;
    int nread;
 
    in = open("file.in", O_RDONLY);
    out = open("file.out", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    if(in != -1 && out != -1)
    {
        while((nread = read(in, block, 1024)) > 0)
            write(out, block, nread);
    }
 
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值