linux系统编程——文件编程


文章目录

  • linux系统编程——文件编程
  • 一、如何创建、修改一个文件
  • 二、linux系统调用的API
  • 三、文件描述符
  • 四、linux系统调用
  • 1、创建文件—creat
  • 2、打开文件—open
  • 3、写操作—write
  • 4、读操作—read
  • 5、关闭文件—close


一、如何创建、修改一个文件

手动修改文件:打开/创建文件→编辑文件→保存文件→关闭文件
linux中使用编程来操作文件,真正了解如何文件编程是很复杂的,但可以引用系统提供的API

二、linux系统调用的API

API名称

操作名

手册命令

打开

open

man 2 open

创建

creat

man 2 creat

读操作

read

man 2 read

写操作

write

man 2 write

光标定位

lseek

man 2 lseek

关闭

close

man 2 close

三、文件描述符

codesys描述文件制作 描述文件制作教程_codesys描述文件制作


文件描述符相当于文件的索引

四、linux系统调用

1、创建文件—creat

引用头文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

函数原型:int creat(const char *pathname, mode_t mode);

返回值:整型,作为文件描述符,失败返回-1

pathname:文件名的绝对路径(缺省为当前路径)
mode:创建模式

创建模式

宏表示

数字访问权限

可读

S_IRUSR

4

可写

S_IWUSR

2

可执行

S_IXUSR

1

可读可写可执行

S_IRWXU

7

代码示例:

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

int main()
{
	int fd;
        fd = creat("/home/admin_/learn/linux/file3",S_IRWXU);
        //在该路径下,建立一个file3文件,模式为可读可写可执行
        if(fd != -1)
        {
        	printf("create file3 successfully\n");
        }else
        {
        	printf("create file3 fail\n");
        }
        return 0;
}
2、打开文件—open

引用头文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

函数原型:
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

返回值:整型,作为文件描述符,失败返回-1

pathname:文件名的绝对路径(缺省为当前路径)
flags:参数

flags

打开标志

O_RDONLY

只读方式打开

O_WRONLY

只写方式打开

O_RDWR

可读可写方式打开

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

int main()
{
        int fd;
        fd = open("./file2",O_RDWR);
        if(fd != -1)
        {
                printf("open file successfully\n");
        }
        return 0;
}

可选参数

作用

O_CREAT

若文件不存在则创建它,使用此选项,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限

O_EXCL

如果同时指定了OCREAT,而文件已经存在,则出错(打开文件失败,返回值为-1)

O_APPEND

每次写时都加到文件的尾端

O_TRUNC

打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0

O_CREAT使用:

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

int main()
{
        int fd;
        fd = open("./file2",O_RDWR);//单纯的打开文件
        if(fd == -1)//如果文件不存在
        {
                printf("open file2 fail\n");
                fd = open("./file2",O_RDWR|O_CREAT,0600);//6:可读可写权限=4+2
                if(fd>0)
                {
                	printf("create file2 successfully\n");
                }
        }
        return 0;
}

O_EXCL使用:

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

int main()
{
        int fd;
        fd = open("./file2",O_RDWR|O_CREAT|O_EXCL,0600);
        if(fd == -1)//若file2文件已存在,creat和excl参数会导致open文件描述符返回-1
        {
                printf("file exists\n");
        }
        return 0;
}

O_APPEND使用:

原文件内有内容时,写入数据需要引用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="wyplmm\n";
        fd = open("./file1",O_RDWR|O_APPEND);
        printf("open success:%d\n",fd);
        int n_write = write(fd,buf,strlen(buf));
        if(n_write != -1)
                printf("write %d byte to file1\n",n_write);
        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="wyplmm\n";
        fd = open("./file1",O_RDWR|O_TRUNC);
        printf("open success:%d\n",fd);
        int n_write = write(fd,buf,strlen(buf));
        if(n_write != -1)
                printf("write %d byte to file1\n",n_write);
        close(fd);
        return 0;
}
3、写操作—write

前提:已经打开了文件
引用头文件:
#include <unistd.h>

函数原型:
ssize_t write(int fd, const void *buf, size_t count);
write的三个参数:文件描述符、无类型的指针(缓冲区)、文件大小

返回值:整型,表示写入的字节数,-1时表示写入失败

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

int main()
{
        int fd;
        char *buf="wyplmm\n";//存放在内存缓冲区
        fd = open("./file1",O_RDWR);
        if(fd == -1)
        {
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0)
                {
                        printf("create file1 success\n");
                }
        }//结合了打开和创建文件的操作
	printf("open success:fd=%d\n",fd);
        //ssize_t write(int fd, const void *buf, size_t count);write函数原型
        write(fd,buf,strlen(buf));//buf内容被写进file1
        //sizeof在linux下定义指针变量是8个字节可能无法完全显示输入内容,需要strlen显示实际字符个数
        close(fd);//使用完必须关闭
        return 0;
}
4、读操作—read

前提:已经打开了文件
引用头文件:
#include <unistd.h>

函数原型:
ssize_t read(int fd, void *buf, size_t count);
read的三个参数:fd指向的文件,读取的内容放入buf指向的内存,读取的字节数

返回值:整型,表示读取的字节数,-1表示读取失败

#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="wyplmm\n";
        fd = open("./file1",O_RDWR);
        if(fd == -1)
        {
        	printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>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));//返回值为-1时写入失败
        if(n_write != -1)
	{
                printf("write %d type to file1\n",n_write);
        }
        
        char *readBuf=(char *)malloc(sizeof(char)*n_write);//给readBuf开辟空间,防止变成野指针
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readBuf,n_write);
        printf("read %d byte,content:%s\n",n_read,readBuf);
	close(fd);
        return 0;
}
*************************
结果:
open file1 failed
create file1 success
open success:fd=3
write 13 type to file1
read 0 byte,content:

上述出现的一个问题,无法读取到数据read 0 byte,content内容空

**原因:**因为上述的操作进行了写入操作,写操作完毕后,光标移动到文章的最末尾,因此读操作时读取的是从光标起的空内容。
解决方法:
1、在读操作前关闭再打开文件

close(fd);
fd = open("./file1",O_RDWR);

2、将光标移动前文本前面
光标定位lseek
引用头文件:
#include <sys/types.h>
#include <unistd.h>

函数原型:
off_t lseek(int fd, off_t offset, int whence);
作用:将文件读写指针(光标)相对whence移动offset个字节


作用

SEEK_SET

指向文件头

SEEK_CUR

指向当前位置

SEEK_END

指向文件尾

#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="wyplmm\n";
        fd = open("./file1",O_RDWR);
        if(fd == -1)
        {
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>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 type to file1\n",n_write);
        }
        //close(fd);第一种方法
        //fd = open("./file1",O_RDWR);第一种方法
        lseek(fd,0,SEEK_SET);//等于lseek(fd,-21,SEEK_CUR);
        char *readBuf=(char *)malloc(sizeof(char)*n_write);
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readBuf,n_write);
        printf("read %d byte,content:%s\n",n_read,readBuf);
        close(fd);
        return 0;
}
5、关闭文件—close

引用头文件:
#include <unistd.h>
函数原型:
int close(int fd);

close(fd);