Linux C 文件编程

一、Linux 文件

Linux文件可分为:普通文件,目录文件,链接文件,设备文件;

当打开一个现存文件或创建一个新文件时,内核就向进程返回一个文件描述符;当需要读写文件时,也需要把文件描述符作为参数传递给相应的函数。

二、Linux文件操作函数(文件I/O)

1. 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,要打开或创建的文件名字
参数2:flags,文件权限

(以下可选项可以同时指定0个或多个, 和必选项按位或起来作为flags参数。)

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

O_NONBLOCK 如果pathname指的是一个块特殊文件或一个字符特殊文件,则此选择项为此文件的本次打开操作和后续的I /O操作设置非阻塞方式。

以下三个常数中必须指定一个,且仅允许指定一个(这些常数定义在<fcntl.h>头文件中))

O_RDONLY   只读打开

O_WRONLY   只写打开

O_RDWR     读、写打开

参数3:mode,当创建文件时候使用。
返回值: 成功返回文件描述符,否则返回-1.

2.close()关闭

3. read()读 

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

fd: 文件描述符
buf:存储将要读入的数据
count: 读出的长度,以字节为单位
返回值:读成功时,返回读出的字符长度,否则返回-1。

4. write()写 

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

fd: 文件描述符
buf:存储将要写的数据
count: 写入的长度,以字节为单位
返回值:写入成功时,返回写入的字符长度,否则返回-1。

5.lseek()修改文件偏移量

int lseek(int fd, off_t offset, int whence); 

fd: 文件描述符
offset:将要偏移 的字节数。取负值表示向前移动。
whence:从哪开始偏移

宏定义如下:
SEEK_END 相对文件末尾
SEEK_CUR 相对文件读写指针当前偏移量位置
SEEK_SET 相对文件开头位置

三、C 文件操作函数(标准I/O)

1. 标准I/O介绍 

不带缓存的I/O对是文件描述符操作,带缓存的I/O是针对流的。

标准I/O库就是带缓存的I/O,它由ANSIC标准说明。当然,标准I/O最终都会调用上面的I/O例程。

标准I/O提供缓存的目的就是减少调用readwrite的次数,它对每个I/O流自动进行缓存管理(标准I/O函数通常调用malloc来分配缓存)。

它提供了三种类型的缓存:

    1) 全缓存。当填满标准I/O缓存后才执行I/O操作。磁盘上的文件通常是全缓存的。
   
2) 行缓存。当输入输出遇到新行符或缓存满时,才由标准I/O库执行实际I/O操作。stdinstdout通常是行缓存的。
   
3) 无缓存。相当于readwrite了。stderr通常是无缓存的,因为它必须尽快输出。

2.fopen()打开文件

FILE *fopen(const char *filename, const char *mode)

filename:打开的文件名(包含路径,缺省为当前路径)

mode:    打开模式

r,  rb 只读方式打开,文件必须已存在
w, wb

只写方式打开,如果文件不存在则创建,

                         如果文件已存在清空重写

a, ab 只能在文件末尾追加数据,如果文件不存在则创建
r+rb+, r+b 读写方式打开,文件必须已存在
w+w+b, wb+

读写方式打开,如果文件不存在则创建,

                         如果文件已存在清空重写

a+, a+b, ab+ 读和追加方式打开,如果文件不存在则创建

 

 3. fclose()关闭文件

int fclose(FILE * stream); 

fclose()用来关闭先前fopen()打开的文件。此动作会让缓冲区内的数据写入文件中,并释放系统所提供的文件资源

返回值:若关文件动作成功则返回0有错误发生时则返回EOF并把错误代码存到errno

4.fseek()文件定位

 int fseek(FILE *stream, long offset, int whence);

stream: 要重新定位的文件指针;
offset: 相对于第三个参数的偏移量;
whence: 文件指针相对于文件的位置。

5.fwrite()写文件

int fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) ;

ptr: 存放想要写入文件的数据;
size: 每块数据的大小;
nmemb: 读取的数据块数;
steam: 希望写入的文件的文件指针;

四、代码示例 

1.Linux:文件读写操作

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

int main()
{
    int fd, ret; //file describtion 文件描述符
    char buf[15] = "hellohello";

#if 0
    fd = open("hello.c", O_RDWR); //以读写方式打开文件hello.c

    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }

    close(fd);
#endif

    fd = open("xx.txt", O_RDWR | O_CREAT, S_IRWXU); //创建文件xx.c并以读写方式打开

    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }

    ret = write(fd, buf, strlen(buf));
    if(-1 == ret)
    {
        perror("write");
        exit(1);
    }


    //ret = lseek(fd, -10, SEEK_END);//相对文件末尾向前移动长度10
    ret = lseek(fd, 0, SEEK_SET);//相对文件首位置向后移动长度0
    if(-1 == ret)
    {
        perror("lseek");
        exit(1);
    }

    memset(buf, 0, sizeof(buf));
    ret = read(fd, buf, sizeof(buf));
    if(ret == -1)
    {
        perror("read");
        exit(1);
    }
    printf("read from txt : %s\n", buf);

    close(fd);
    return 0;
}

2.Linux:用命令行参数编写拷贝文件函数

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

int main(int argc, char *argv[])
{
    int fd_from, fd_to, ret; //file describtion 文件描述符
    char buf[32] = {0};

    if(argc != 3)  //入参判断
    {
        printf("Input Error!\n");
        exit(1);
    }

    fd_from = open(argv[1], O_RDONLY);  //只读方式打开第一个文件
    if(-1 == fd_from)
    {
        perror("open1");
        exit(1);
    }
    printf("%d\n", fd_from);


    fd_to = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
    if(-1 == fd_to)
    {
        perror("open2");
        exit(1);
    }
    printf("%d\n", fd_to);

    while((ret = read(fd_from, buf, sizeof(buf) - 1)) > 0)
    {
        ret = write(fd_to, buf, ret);
        if(-1 == ret)
        {
            perror("write");
            exit(1);
        }
        memset(buf, 0, sizeof(buf));
    }


    close(fd_from);
    close(fd_to);
    return 0;
}

3.C:文件读写

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *fp;
    int ret;
    char buf[32] = {0};

    fp = fopen("hello.c", "a+"); //读和追加的方式打开hello.c,不存在则创建
    if(NULL == fp)
    {
        perror("fopen");
        exit(1);
    }

    ret = fseek(fp, 0, SEEK_SET);
    if(-1 == ret)
    {
        perror("fseek");
        exit(1);
    }

    ret = fread(buf, 1, sizeof(buf), fp);
    if(-1 == ret)
    {
        perror("fread");
        exit(1);
    }
    printf("read from txt : %s\n", buf);

    memset(buf, 0, sizeof(buf));
    scanf("%s", buf);
    ret = fwrite(buf, 1, strlen(buf), fp);
    if(ret < 0)
    {
        perror("fwrite");
        exit(1);
    }

    fclose(fp);

    return 0;
}

4. C:求文件长度

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    FILE *fp;

    if(argc != 2)
    {
        printf("Input Error!\n");
        exit(1);
    }

    fp = fopen(argv[1], "r");

    if(NULL == fp)
    {
        perror("fopen");
        exit(1);
    }

    fseek(fp, 0, SEEK_END);  //文件指针移动到文件末尾

    int length = ftell(fp);
    printf("length is %d\n", length);

    fclose(fp);
    return 0;
}

5.Linux:将结构体存入文件 

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

struct Student
{
	char name[20];
	int age;
	char sex;
};

int main()
{
	struct Student s1 = {"aaaa", 20, 'm'};
	struct Student s2 = {"bbbb", 30, 'f'};

	int fd = open("Student.txt", O_WRONLY | O_CREAT | O_EXCL, 0666);
	if (-1 == fd)
	{
		perror("open");
		exit(1);
	}

	int ret = write(fd, (void *)&s1, sizeof(s1));
	if (-1 == ret)
	{
		perror("write");
		exit(1);
	}

	ret = write(fd, (void *)&s2, sizeof(s1));
	if (-1 == ret)
	{
		perror("write");
		exit(1);
	}

	struct Student s;
	
	fd = open("Student.txt", O_RDONLY);
	if (-1 == fd)
	{
		perror("open");
		exit(1);
	}

	while (1)
	{
		ret = read(fd, (void *)&s, sizeof(s));
		if (-1 == ret)
		{
			perror("read");
			exit(1);
		}
		if (ret == 0)
		{
			break;
		}
		printf("%s %d %c\n", s.name, s.age, s.sex);
		memset(&s, 0, sizeof(s));
	}

	close(fd);
	return 0;
}
 

猜你喜欢

转载自blog.csdn.net/qq_41998576/article/details/81636159