Linux系统编程1--基本文件操作(open、w/r、lseek、creat)

Linux系统编程1–基本文件操作(open、w/r、lseek、creat

常用命令

  • Ctrl + Shift + 放大
  • Ctrl - 缩小
  • yy粘贴,p复制
  • dd删除 num dd
  • u撤销
  • ./当前目录下的文件
  • Ctrl Alt t 打开终端
  • tab自动补全
  • mv移动,mv *[] 剪切、mv a.c b.c 重命名
  • set nu 显示行标

命令

  • pwd显示当前文件夹路径
  • sudo 管理员权限
  • mkdir 建立文件夹
  • cd 进入某个文件夹
  • cd ..退出某个文件夹
  • ls列出文件夹
  • ls -a列出所有文件夹
  • cp a.c b.c a b 拷贝
  • cd回到工作目录
  • touch file1 创建新的空文件
  • rm 删除
  • ls -l 单列输出
  • man 2 write查看Linux中的指令帮助 /read

1、文件编程

  • 打开: open
  • 读写: write / read
  • 光标定位: lseek
  • 关闭: close
  • 创造: creat

1.1 open 打开

int open(const char *pathname, int flags);

int open(const char *pathname, int flags,mode_t mode);

返回文件描述符 fd ,非负代表成功,-1代表失败

  • 打开成功:返回值不为 -1;
  • 打开失败:返回值为 -1;
  • 添加头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcnt1.h>

请添加图片描述

  • flag: 三选一
    • O_RDONLY :只读
    • O_WRONLY:只写打开
    • O_RDWE: 可读可写打开
  • O_CREAT:文件不存在,这创建;
//demo2.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
	int fd;  //open函数返回的文件描述符 fd
	fd = open("./file1",O_RDWR);

	if(fd == -1) //返回值是 -1,表示失败!
	{
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT,0600);
		//O_RDWE|O_CREAT 表示如果文件不存在,创建这个文件&加权限0600
        //可读r 4    可写w 2    执行x 1       0600指的是 4+2
		if(fd > 0)//成功,返回值大于0
		{
			printf("create file1 success!\n");
		}
	}
	printf("fd = %d\n",fd);
	return 0;
}

O_EXCL:文件存在,则报错;//用来判断文件是否存在;

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

int main()
{
    int fd;
    
    fd = open("./file",O_RDWR|O_CREAT|O_EXCL,0600); //不存在生成,存在就报错,可以判断文件是否存在
    if(fd == -1)
    {
        printf("file exist!");
    }
  
    return 0;
}

O_APPEND:每次写的时候都加到文件的尾端;

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> //write写入的头文件
#include <string.h> //strlen函数所需头文件
#include <stdlib.h>

int main()
{
    int fd;
    char *buf = "tp22 nb666";
    
    fd = open("./file1",O_RDWR|OAPPEND);//每次写的时候都加到文件的尾端;
    printf("open success: fd = %d\n",fd);
    
    int n_write = write(fd,buf,strlen(buf));
    if(n_write != -1)
    {
        printf("write %d byte to file\n",n_write);
    }
    close(fd);
    
    return 0;
}

O_TRUNC:把原来所有的内容都干掉,将长度截断为0

1.2 write 写入

ssize_t write(int fd, const void *buf, size_t count);

写入成功:返回值 count; 写入失败:返回值为1;

  • 中间的参数 ,buf不一定要是字符串,也可以是地址,(字符串也可以理解为地址)
const viod *buf 
//也可以是地址
int data = 100int = n_write = write(fd, &data,sizeof(int));
  • 添加头文件
#include<unistd.h>

请添加图片描述

count 个来自 buf 里的数写入到 fd

  • 演示
//demo3.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> //write写入的头文件
#include <string.h> //strlen函数所需头文件

int main()
{
	int fd;  //open函数返回的文件描述符 fd
	char *buf = "tp666 nb666"; //字符串在编译器里面是 地址
	fd = open("./file1",O_RDWR);

	if(fd == -1) //返回值是 -1,表示失败!
	{
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT,0600);
		//O_RDWE|O_CREAT 表示如果文件不存在,创建这个文件&加权限0600
        //可读r 4    可写w 2    执行x 1       0600指的是 4+2
		if(fd > 0)//成功,返回值大于0
		{
			printf("create file1 success!\n");
		}
	}
	printf("open success :fd = %d\n",fd);//fd = 3
//写入
	//ssize_t write(int fd,const void *buf,size_t count);
	write(fd,buf,strlen(buf)); //写入文件,将buf里的数据写入到 file1 文件中
    

	close(fd); //关闭文件
	
	return 0;
}

1.3 read 读取

ssize_t read(int fd, void *buf, size_t count);

fd 指向的文件读取 count 个字节的数据放在 buf

  • 读取成功:返回读取数;
  • 读取失败:返回 -1;
  • 添加头文件
#include<unistd.h>

请添加图片描述

  • 先写入,后读取 的光标问题

    • 光标移到头,再读(下一小节介绍)
    • 重新打开(这一小节用这个)
  • 演示

//demo4.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> //write写入的头文件
#include <string.h> //strlen函数所需头文件
#include <stdlib.h> //malloc函数

int main()
{
	int fd;  //文件描述符 //open函数的返回值
	char *buf = "tp666 nb666"; //字符串在编译器里面是 地址
	
	fd = open("./file1",O_RDWR);

	if(fd == -1) //返回值是 -1,表示失败!
	{
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT,0600);
		//O_RDWE|O_CREAT 表示如果文件不存在,创建这个文件&加权限0600
		if(fd > 0)//成功,返回值大于0
		{
			printf("create file1 success!\n");
		}
	}
	printf("open success :fd = %d\n",fd);

//写入
	//ssize_t write(int fd,const void *buf,size_t count);
	int n_write = write(fd,buf,strlen(buf)); //写入文件,返回写入字节数
	if(n_write != -1)
	{
		printf("write %d byte to file1\n",n_write);
	}
	
//读取
	//读取前,处理以下光标,重新打开
	close(fd);//关闭
	fd = open("./file1",O_RDWR);//重新打开,光标初始化

	char *readBuf;
	readBuf = (char *)malloc(sizeof(char)*n_write + 1);//防止断错误
	//ssize_t read(int fd, void *buf, size_t count);
	int n_read = read(fd, readBuf, n_write);
    //从 fd 所指向的文件中,读取 n_write 个字节的数据,放在 readBuf 里面
	printf("read %d,context:%s\n",n_read,readBuf);
	
	close(fd); //关闭文件
	return 0;
}

1.4 lseek 光标的移动

off_t lseek(int fd, off_t offset, int whence);
  • 将文件读写指针相对 whence 移动 offset 个字节;

  • 成功 返回 偏移值 offset;//针对文件头,偏移多少个字节;

     //计算文件大小
     int fd;
     char *buf = "tp222 nb666";
     
     fd = open("./file1",O_RDWR);
     int filesize = lseek(fd, 0, SEEK_END);
     printf("file`s size is %d\n",filesize);
    

失败 返回 -1;

  • whence :固定点的一个位置;

    • SEEK_SET : 文件的头
    • SEEK_END: 文件的尾
    • SEEK_CUR: 当前光标位置
  • offset : 对 whence 的一个偏移值

    • 正数往前走;
    • 复数往后走;
  • 添加头文件

#include <sys/types.h>
#include <unistd.h>

请添加图片描述

  • 演示
//demo5.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> //write写入的头文件
#include <string.h> //strlen函数所需头文件
#include <stdlib.h> //malloc函数

int main()
{
	int fd;  //文件描述符 //open函数的返回值
	char *buf = "tp666 nb666"; //字符串在编译器里面是 地址
	
	fd = open("./file1",O_RDWR);

	if(fd == -1) //返回值是 -1,表示失败!
	{
		printf("open file1 failed\n");
		fd = open("./file1",O_RDWR|O_CREAT,0600);
		//O_RDWE|O_CREAT 表示如果文件不存在,创建这个文件&加权限0600
		if(fd > 0)//成功,返回值大于0
		{
			printf("create file1 success!\n");
		}
	}
	printf("open success :fd = %d\n",fd);

//写入
	//ssize_t write(int fd,const void *buf,size_t count);
	int n_write = write(fd,buf,strlen(buf)); //写入文件,返回写入字节数
	if(n_write != -1)
	{
		printf("write %d byte to file1\n",n_write);
	}
	

//读取
	//读取前,处理以下光标,重新打开
	//close(fd);//关闭
	//fd = open("./file1",O_RDWR);//重新打开,光标初始化
	
	char *readBuf;
	readBuf = (char *)malloc(sizeof(char)*n_write + 1);//防止断错误

	//off_t lseek(int fd, off_t offset, int whence);//offset:正数往前移,负数往后移
	lseek(fd,0,SEEK_SET);//针对文件头的偏移值是 0;
	//lseek(fd,-1*n_write, SEEK_CUR);//也可以这样

	//ssize_t read(int fd, void *buf, size_t count);
	int n_read = read(fd, readBuf, n_write);
        //从 fd 所指向的文件中,读取 n_write 个字节的数据,放在 readBuf 里面
	
	printf("read %d,context:%s\n",n_read,readBuf);
	
	close(fd); //关闭文件
	
	return 0;
}

1.5 create 创造

int creat(const char *filename, mode_t mode)
  • mode:创建模式
    • S_IRUSR 4 可读
    • S_IWUSR 2 可写
    • S_IXUSR 1 可执行
    • S_IRWXU 7 可读、写、执行
  • 添加头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcnt1.h>

请添加图片描述

  • 演示
#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("/home/tp/桌面/program/file1",S_IRWXU); //可读、写、执行

	return 0;
}

  • 29
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你回答这个问题。 下面是一个简单的示例程序,演示如何在 Linux 下进行文件 I/O 操作: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { char buf[1024]; int fd; // 创建文件 test.c fd = open("test.c", O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); if (fd == -1) { perror("open"); exit(1); } // 打开文件 /home/guestuser1/work/file2 fd = open("/home/guestuser1/work/file2", O_WRONLY); if (fd == -1) { perror("open"); exit(1); } // 写入数据 "Hello" if (write(fd, "Hello", 5) == -1) { perror("write"); exit(1); } // 读取数据 lseek(fd, 0, SEEK_SET); if (read(fd, buf, 5) == -1) { perror("read"); exit(1); } buf[5] = '\0'; printf("Read: %s\n", buf); // 关闭文件 close(fd); return 0; } ``` 在这个程序中,我们首先使用 `open()` 函数创建文件 `test.c`,并使用 `O_CREAT` 和 `O_TRUNC` 标志来指定文件不存在时创建文件文件存在时截断文件(清空文件内容)。然后,我们使用 `open()` 函数打开文件 `/home/guestuser1/work/file2`,并使用 `O_WRONLY` 标志来指定只写模式。接着,我们使用 `write()` 函数在文件中写入数据 "Hello",并使用 `read()` 函数读取数据。最后,我们使用 `close()` 函数关闭文件。 注意,在实际的编程中,我们需要对每个文件操作进行错误检查,并适当地处理错误情况。另外,在 Linux 下进行文件 I/O 操作时,还需要注意文件权限的设置,以确保文件能够被正确地读取和写入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值