系统编程—文件编程

一: 文件编程

  1. 系统调用——》调用系统写好的函数接口(一定是操作系统提供的只能在LINUX系统下)

  2. 用火编程接口(API)——》application program interface

  3. 文件描述符——》代表一个文件

  4. 一个进程启动时候, 都会代开3个文件
    ( 标准输入 标准输出 标准出错处理)
    0 1 2 文件描述符
    STDIN_FILENO STDOUT_FILENO STDERR_FILENO 宏

  5. ulimit -n 100 修改进程打开的文件数量(进程打开的文件是有限的 MAX 1024)

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

(2)文件描是一个非负的整数,它是一个索引值,并指向在内核中每个进程打开文件的记录表。

(3)一个进程启动时,都会打开3个文件:标准输入、标准输出和标准出错处理

  1. 系统调用之创建
    int creat(const char *filename, mode_t mode )
    filename :创建的文件名
    (包含路径,缺省为当前路径)
    mode:创建模式
    常创建模式:
    S_IRUSR 可读
    S_IWUSR 可写
    S_IXUSR 可执行
    S_IXRWU 可读、可写、可执行 除用以上宏来选择创建模式,也可以用数字来表示

  2. 参数的含义
    参数:
    pathname是要打开或创建的文件的名字
    flags参数可用来说明此函数的多个选择项
    mode对于open函数而言,仅当创建新文件时才使用第三个参数

返回值:成功返回新分配的文件描述符,
出错返回-1并设置errno

  1. mode 参数
    以下三个常数中必须指定一个,且仅允许指定一个(这些常数定义在<fcntl.h>头文件中)
    O_RDONLY 只读打开
    O_WRONLY 只写打开
    O_RDWR 读、写打开

10.系统调用——定位
whence可使用下述值:
SEEK_SET:相对文件开头
SEEK_CUR:相对文件读写指针的当前位置
SEEK_END:相对文件末尾
offset可取负值,表示向前移动。例如下述调用
可将文件指针相对当前位置向前移动5个字节:
lseek(fd, -5, SEEK_CUR)

11.缓存的相关知识
(1)不带缓存的I/O对是文件描述符操作,带缓存的I/O是针对流的。

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

(3)标准I/O库代替用户处理很多细节,比如缓存分配、以优化长度执行I/O等。

(4)标准I/O提供缓存的目的就是减少调用read和write的次数,它对每个I/O流自动进行缓存管理(标准I/O函数通常调用malloc来分配缓存)。
它提供了三种类型的缓存:
1) 全缓存。当填满标准I/O缓存后才执行I/O操作。磁盘上的文件通常是全缓存的。
2) 行缓存。当输入输出遇到新行符或缓存满时,才由标准I/O库执行实际I/O操作。stdin、stdout通常是行缓存的。
3) 无缓存。相当于read、write了。stderr通常是无缓存的,因为它必须尽快输出。

二 : 代码实现

一: creat (LINUX1——》creat.c)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>

int main()
{
int fd;

fd = creat("hello.txt", S_IRUSR | S_IWUSR);       //1.文件名  2. 可读   3. 可写
if (-1 == fd)
{
	perror("creat");   //打印错误信息
	exit(1);
}

return 0;

}

二: open (LINUX——》open.c)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main()
{
int fd;

/*//文件必须存在
fd = open("hello.txt", O_RDWR);   //打开文件  O_RDWR打开的方式(只读、只写、读写) 
if (-1 == fd)
{
	perror("open");
	exit(1);
}*/

//文件不存在,创建并打开文件
//O_CREAT  创建文件
//S_IRWXU  创建的文件的权限 当前用户有可读可写可执行的权限
//O_EXCL   文件必须不存在,否则报错
fd = open("hello.txt", O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
if (-1 == fd)
{
	perror("open");
	exit(1);
}

close(fd);   //关闭文件

return 0;

}

三: write(Linux1——》write.c)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main()
{
int fd, ret;
char buf[32] = “helloworld”;

fd = open("hello.txt", O_WRONLY);   //只写方式打开文件(文件存在)
if (-1 == fd)
{
	perror("open");
	exit(1);
}

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

close(fd);

return 0;

}

四: read(linux1——》read.c)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
int fd = open(“hello.txt”, O_RDONLY);
if (-1 == fd)
{
perror(“open”);
exit(1);
}

char buf[32] = {0};
int ret = read(fd, buf, sizeof(buf));
if (-1 == ret)
{
	perror("read");
	exit(1);
}

printf("read from txt : %s, length %d\n", buf, ret);

return 0;

}

五:lseek(linux——》lseek.c)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char buf[32] = “helloworld!”;
int fd = open(“hello.txt”, O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
if (-1 == fd)
{
perror(“open”);
exit(1);
}

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

//lseek(fd, -11, SEEK_CUR);   //移动文件指针  相对当前指针位置,向前移动10个字节
//lseek(fd, 0, SEEK_SET);     //相对于文件开头,移动0个字节
ret = lseek(fd, -11, SEEK_END);     //相对于文件末尾,向前移动11个字节
printf("%d\n", ret);

memset(buf, 0, sizeof(buf));   //清空buf

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

return 0;

}

六 copy.c(linux1——》copy.c)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
if (argc != 3)
{
printf(“ERROR!\n”);
exit(1);
}

int fd_from, fd_to, ret;

fd_from = open(argv[1], O_RDONLY);  //只读方式打开已经存在的文件
if (-1 == fd_from)
{
	perror("open1");
	exit(1);
}

fd_to = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 00700);
if (-1 == fd_to)
{
	perror("open2");
	exit(1);
}

char buf[32] = {0};
while ((ret = read(fd_from, buf, sizeof(buf) - 1)) != 0)
{
	ret = write(fd_to, buf, ret);
	if (-1 == ret)
	{
		perror("write");
	}

	memset(buf, 0, sizeof(buf));
}

close(fd_from);
close(fd_to);

return 0;

}

七 length(linux1——》length.c)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
int fd = open(argv[1], O_RDWR);
if (-1 == fd)
{
perror(“open”);
exit(1);
}

int ret = lseek(fd, 0, SEEK_END);
printf("%d\n", ret);

close(fd);

return 0;

}

八 struct(linux1——》struct.c)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

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

int main()
{
struct test t1 = {“aaa”, 10, ‘f’, “12345”};

int fd = open("temp.txt", O_WRONLY);

write(fd, &t1, sizeof(t1));

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值