Linux文件编程

Linux文件编程

  1. 文件打开及创建
  2. 文件写入、读取操作
  3. 文件光标移动操作

一、文件打开及创建

1.若文件存在

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
//  int open(const char *pathname, int flags);
//                文件的路径         权限
//  int open(const char *pathname, int flags, mode_t mode);
//              文件的路径                      权限                    
//      int creat(const char *pathname, mode_t mode);
//      fd = creat ("/mnt/hgfs/LinuxStudy/file2",S_IRWXU);                                      
//                                      
//      creat()创建文件,返回值文件描述符。                                           
//      open()的返回值 : 一个文件描述符小的非负整数fd,后面的一系列操作都要通过fd。    失败返回-1。
//      权限:O_RDONLY 只读打开         O_WRONLY 只写打开       O_RDWR 可读可写打开(常用)
//      以上权限三选一,以下可选的权限:
//      1、O_CREAT      若文件不存在则创建文件。要说明第三个参数mode,用来说明该文件的存取的可许权限。
//      2、O_EXCL       配合O_CREAT使用,如果文件不存在,则创建文件。如果文件已经存在,返回-1。用来判断文件是否>存在。
//      3、O_APPEND     每次写时都加到文件的尾端。(追加)。
//      4、O_TRUNC      把文件里原先的内容都删掉。
//      touch file:创建文件的指令
        int fd;
        fd = open("./file1",O_RDWR);

        printf("fd = %d\n",fd);

        return 0;
}

  1. 若文件不存在
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
        int fd;
//      ls -l:列出文件清单
//      -                       rwxr           -xr-x:
//      普通文件         当前用户对该文件的权限(r可读,w可写,x可执行)
//      可读:4 可写:2 可执行:1               0600:可读+可写
        fd = open("./file1",O_RDWR);
        if(fd == -1)
        {
                printf("open fail!\n");

                fd = open("./file1",O_RDWR | O_CREAT,0600);

                if(fd > 0){
                        printf("creat successfully!\n");
                }
        }
        printf("fd = %d\n",fd);

        return 0;
}

O_CREAT 若文件不存在则创建文件。要说明第三个参数mode,用来说明该文件的存取的可许权限。

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

int main()
{
	int fd;
	char *buf = "Test";
//	int creat(const char *pathname, mode_t mode);
	
	fd = creat ("/mnt/hgfs/LinuxStudy/file2",S_IRWXU);
		
	
	return 0;
}

O_EXCL 配合O_CREAT使用,如果文件不存在,则创建文件。如果文件已经存在,返回-1。用来判断文件是否存在。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
	int fd;

	fd = open("./file1",O_RDWR | O_CREAT | O_EXCL,0600);
		
	if(fd == -1){
		printf("file exist!\n");
	}

	return 0;
}

运行结果:
在这里插入图片描述
在这里插入图片描述

O_APPEND 每次写时都加到文件的尾端。(追加)。

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

int main()
{
	int fd;
	char *buf = "ChenZhihong is handsome!";

	fd = open("./file1",O_RDWR | O_APPEND);
	printf("open successfully : fd = %d\n",fd);
	int n_write = write(fd,buf,strlen(buf));
	
	if(n_write != -1){
		printf("write %d byte, context :%s\n",n_write,buf);
	}
	close (fd);
	return 0;
}

O_TRUNC 把文件里原先的内容都删掉,重新写入新的内容到文件中

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

int main()
{
	int fd;
	char *buf = "Test";

	fd = open("./file1",O_RDWR | O_TRUNC);
	printf("open successfully : fd = %d\n",fd);
	int n_write = write(fd,buf,strlen(buf));
	
	if(n_write != -1){
		printf("write %d byte, context :%s\n",n_write,buf);
	}
	close (fd);
	return 0;
}

二、文件写入、读取操作

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

int main()
{
	int fd;
	char *buf = "ChenZhihong is handsome!";

	fd = open("./file1",O_RDWR);
	if(fd == -1)
	{
		printf("open fail!\n");

		fd = open("./file1",O_RDWR | O_CREAT,0600);
		
		if(fd > 0){
			printf("creat successfully!\n");
		}
	}
	printf("fd = %d\n",fd);

//ssize_t write(int fd, 	const void *buf, 	size_t count);
//		文件描述符	无类型指针(缓冲区)	写入数据的大小
//write()将缓冲区的数据通过文件描述符写count个字节到fd所指向的文件中
//返回值:成功返回写入的字节数,失败返回-1。
	int n_write = write (fd,buf,strlen(buf));
	if(n_write != -1){
		printf("write %d byte to file1\n",n_write);
	}
//ssize_t read(int fd, 		void *buf, 			size_t count);
//		文件描述符	无类型指针(缓冲区)	读出数据的大小
//read()从fd所指向的文件中读count个字节到缓冲区里。
//返回值:成功返回读出的字节数,失败返回-1。
	char *readbuf;
	readbuf = (char *)malloc(sizeof(char)*n_write + 1);
	int n_read = read (fd,readbuf,n_write);

	printf("read %d,context: %s",n_read,readbuf);	
	close (fd);
	return 0;
}

上面代码会产生一个问题。————光标移动问题
解决方法一:关闭文件之后,重新打开文件
解决方法二:把光标移到文件头,在读文件

三、文件光标移动操作

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

int main()
{
	int fd;
	char *buf = "ChenZhihong is handsome!";

	fd = open("./file1",O_RDWR);
	if(fd == -1)
	{
		printf("open fail!\n");

		fd = open("./file1",O_RDWR | O_CREAT,0600);
		
		if(fd > 0){
			printf("creat successfully!\n");
		}
	}
	printf("fd = %d\n",fd);
	
	int n_write = write (fd,buf,strlen(buf));
	if(n_write != -1){
		printf("write %d byte to file1\n",n_write);
	}

//off_t lseek(	int fd, 	off_t offset, 	int whence);
//				文件描述符		偏移值			
//whence: SEEK_SET 指向文件头、SEEK_END 指向文件尾、SEEK_CUR 当前光标位置
//lseek():将文件读写指针相对于whence移动offset个字节。
//偏移值:负数往前走,正数往后走
//返回值:返回相对于文件头偏移的字节数,失败返回-1。
//用lseek计算文件大小	:int filesize = lseek(fd,0,SEEK_END);


	char *readbuf;
	readbuf = (char *)malloc(sizeof(char)*n_write + 1);
//	lseek(fd,0,SEEK_SET);
	lseek(fd,-24,SEEK_CUR);

	int n_read = read (fd,readbuf,n_write);

	printf("read %d,context: %s \n",n_read,readbuf);	
	close (fd);
	return 0;
}

用lseek计算文件大小

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

int main()
{
	int fd;
	char *buf = "ChenZhihong is handsome!";

	fd = open("./file1",O_RDWR);
//用lseek计算文件大小	
	int filesize = lseek(fd,0,SEEK_END);

	printf("file's size : %d\n",filesize);	

	close (fd);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值