文件概述
文件:存储在外部介质上的数据的集合,是操作系统数据管理的单位
- 文件分类
- 按逻辑结构分
- 记录文件:由具有一定结构的记录组成(定长和不定长)
- 流式文件:有一个个字符(字节)数据顺序组成
- 按存储介质分
- 普通文件:存储截至文件(磁盘、次贷等)
- 设备文件:非存储介质(键盘、显示器、打印机等)
- 组织形式分
- 文本文件:ASCII文件,每个字节存放一个字符的ASCII码
- 二进制文件:数据按其在内存中的存储形式原样存放
- 按逻辑结构分
在Linux下一切皆文件。
![](https://img.haomeiwen.com/i5688965/93712dd2b7d89e82.png)
文件的处理方法
-
缓冲文件系统:高级文件系统,系统自动为正在使用的我呢件开辟内存缓冲区。
缓冲文件系统示意图
-
非缓冲文件系统:低级文件系统,由用户在程序中为每个文件设定缓冲区
非缓冲文件系统
-
文件类型指针
- 指针变量说明:
FILE *fp;
-
用法:
- 文件打开时,系统自动建立文件结构体,并把指向他的指针返回来,程序通过这个指针获得文件信息,访问文件
-
文件关闭后,他的文件结构体被释放
-
FILE结构体的源代码
-
- 指针变量说明:
typedef struct _iobuf{
char* _ptr; //文件输入的下一个位置
int _cnt; //当前缓存区的相对位置
char* _base; //指基础位置
int _flag; //文件标志
int _file; //文件的有效性验证
int _charbuf; //检查缓冲区的状况,如果无缓冲区则不读取
int _bufsiz; //文件的大小
char* _tmpfname; //临时文件名
} FILE;
对底层文件的信息进行了封装。
-
C语言操作文件的库函数的实现,包装在
stdio.h
中
文件使用方法:打开文件 -> 文件读/写 -> 关闭文件
-
系统自动打开和关闭三个标准文件:
- 标准输入——键盘
stdin
- 标准输出——显示器
stdout
- 标准错误输出——显示器
stderr
- 标准输入——键盘
-
打开文件
fopen
函数原型:FILE *fopen(char *name, char *mode)
- 功能:按指定方式打开文件
- 返回值:正常打开,为执行文件的结构体指针;打开失败,为NULL。
-
打开的模式
-
r/rb
(只读):输入打开一个文本或而二进制文件
-
w/wb
(只写):输出打开一个文本或而二进制文件 -
a/ab
(追加):向文本或二进制文件末尾追加数据 -
r+/rb
(读写):读、写打开一个文本或而二进制文件(虽然可以写,但文件必须存在) -
w+/wb+
(读写):读、写建立一个文本或而二进制文件 -
a+/ab+
(读写):读、写打开或建立一个文本或而二进制文件
-
//例如:只读方式打开
#include<stdio.h>
//.......
FILE *fp1;
fp1 = fopen("test.dat". "r");
char* filename = "test.dat";
fp1 = fopen(filename, "r");
//例如:读写方式打开,打开错误程序退出
FILE* fp2;
fp2 = fopen(filename, "rw");
if(fp2 == NULL)
{
printf("File open error!\n");
exit(0);
}
-
文件关闭
fclose
- 函数原型:
int fclose(FILE *fp);
- 作用:使文件指针变量与文件“脱钩”,释放文件结构体和文件指针
- 功能:关闭FILE*所指向的文件
-
返回值:正常关闭返回0;否则返回非0值。
fclose
- 函数原型:
-
文件读写
- 字符I/O:
fputc
和fgetc
-
fputc
- 函数原型:
int fputc(int c, FILE* fp)
- 功能:把一字节代码c,写入fp所指向的文件中
- 返回值:正常返回c,错误返回EOF(文件结束符)
- 函数原型:
-
fgetc
- 函数原型:
int fgetc(FILE* fp)
- 功能:从fp指向的文件中读取一字节的内容
- 返回值:正常,返回读到的代码值;读取到文件结尾或出错,返回EOF
- 函数原型:
-
- 字符I/O:
-
文件I/O与终端I/O
#define putc(ch, fp) fputc(ch, fp)
//putc实际是fputc的宏定义
#define getc(fp) fgetc(fp)
//getc实际是fgetc的宏定义
#define putchar(c) fputc(c, stdout)
//常用的putchar其实是fputc在stdout的宏定义
#define getchar() fgetc(stdin)
//常用的getchar实际是fgetc在stdin的宏定义
例如,读取文件并显示
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE* fp;
char ch, *filename = "out.txt";
if((fp = fopen(filename, "r")) == NULL){
printf("cannot open file\n");
exit(0);
}
while((ch = fgetc(fp)) != EOF)
putchar(ch);
fclose(fp);
return 0;
}
//将屏幕输入写入文件中,如果为$则结束输入
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *pf;
pf = fopen("./test.dat", "w");
if(pf == NULL)
{
printf("error\n");
exit(0);
}
int x = 0;
while((x = getchar()) != '$')
{
putchar(x);
fputc(x, pf);
x = 0;
}
fclose(pf);
return 0;
}
![](https://img.haomeiwen.com/i5688965/285b2b7b79d5a7c9.png)
由执行结果可以看出,如果未输入$那么程序不会跳出while循环,则不会执行到
fclose
,那么文件中则不会有内容。因此fclose
具备刷新缓冲区到硬盘的作用。
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *pf;
pf = fopen("./test.dat", "w");
if(pf == NULL)
{
printf("error\n");
exit(0);
}
int x = 0;
while((x = getchar()) != '$')
{
putchar(x);
fputc(x, pf);
x = 0;
fflush(pf); //手动的刷新缓存到硬盘
}
fclose(pf);
return 0;
}
![](https://img.haomeiwen.com/i5688965/1b8475c363f25be9.png)
- 数据块I/O:fread和fwrite
- 函数原型
size_t fread(void* buffer, size_t size, size_t count, FILE *fp)
size_t fwrite(void* buffer, size_t size, size_t count, FILE *fp)
- 功能:读/写数据块
- 返回值:成功,返回读/写的块数;出错或文件尾,返回0
- 说明:
typedef unsigned size_t
buffer:指向要输入/输出数据块的首地址的指针
size:每个要读/写的数据块的大小(字节数)
count:要读/写的数据块的个数
fp: 要读/写的文件指针
fread/fwrite:一般用于二进制文件的输入/输出。
- 函数原型
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct t
{
int num;
int count;
} test;
int main()
{
FILE* fp;
if((fp = fopen("test.dat", "w")) == NULL)
{
printf("error\n");
exit(0);
}
int i, j;
test ts[5];
for(i =0; i < 5; i++){
ts[i].num = i;
ts[i].count = i * 100;
printf("num = %d; count = %d \n", ts[i].num, ts[i].count);
}
// 采用逐个存放的方法存放,也可以一次性把数组直接存入,那么代码是 fwrite(ts, 1, sizeof(ts), fp);
for(j = 0; j < 5; j++){
fwrite(&ts[j], 1, sizeof(test), fp);
}
fclose(fp);
FILE* pf;
if((pf = fopen("test.dat", "r")) == NULL){
printf("error\n");
exit(0);
}
int k;
test ts2[5];
//之前是逐个放入的,这里一次性取出,也可以逐个取出
fread(ts2, 5, sizeof(test), pf);
for(k = 0; k < 5; k++){
printf("num = %d; count = %d \n", ts2[k].num, ts2[k].count);
}
fclose(pf);
return 0;
}
![](https://img.haomeiwen.com/i5688965/849fc924ce102887.png)
- 格式化I/O:fprint和fscanf
- 函数原型
int fprintf(FILE *fp, const char* format[, argument,...])
int fscanf(FILE *fp, const char* format[, address...])
- 功能:按格式对文件进行I/O操作
- 返回值:成功返回I/O的个数;出错或文件尾,返回EOF
- 函数原型
这两个函数的使用和scanf和printf基本一致,无非第一个参数为文件指针。同样如果fscanf的第一个参数为stdin,那么就相当于scanf;如果fprintf的第一个参数为stdout,那么就相当于printf
fprintf(fp, "%d, %d6.2f", i, t);//将i、t按照%d,%6.2f的格式输出到fp文件中
fscanf(fp, "%d, %f", &i, &t);//若文件中有3,4.5,则3送入i,4.5送入t
#include<stdio.h>
int main()
{
char s[80], c[80];
int a, b;
FILE *fp;
if((fp = fopen("test", "w")) == NULL){
puts("can't open file\n");
exit(0);
}
fscanf(stdin, "%s%d", s, &a); //从键盘读取
fprintf(fp, "%s %d", s, a); //写到文件
fclose(fp);
if((fp = fopen("test", "r")) == NULL){
puts("can't open file\n");
exit(0);
}
fscanf(fp, "%s%d", c, &b); //从文件读取
fprintf(stdout, "%s %d", c, b); //打印到屏幕
return 0;
}
-
字符串I/O:
fputs
和fgets
- 函数原型
char* fgets(char *s, int n, FILE *fp)
fgets从fp所指文件读n-1个字符送入s指向的内存区,并在最后加一个“\0”。
若读入n-1个字符前与换行符或文件尾(EOF)即结束。
int fput(char *s, FILE *fp)
fput把s所指向的字符串写入fp所指向的文件 - 功能:从fp指向的文件读/写一个字符串
- 返回值:
- fgets正常时返回读取字符串的首地址;出错或文件尾,返回NULL
- fputs正常时返回写入的最后一个字符;出错为EOF
- 函数原型
-
文件的定位
- 几个概念
- 文件位置指针——指向当前读写位置的指针
- 读写方式
- 顺序读写:位置指针按字节位置顺序移动
- 随机读写:位置指针按需要移动到任意位置
- rewind函数
- 函数原型
void rewind(FILE *fp)
- 功能:重置文件位置指针到文件开头
- 返回值:void
- 函数原型
//显示,复制文件中的内容
include<stdio.h>
int main(){
FILE *fp1, *fp2;
fp1 = fopen("test.dat", "r");
fp2 = fopen("target.dat", "w");
while(!eof(fp1)) putchar(getc(fp1));
//fp1的指针已指向文件末尾,如果不rewind,则从末尾开始执行后续代码,写入不了内容。
rewind(fp1);
while(!feof(fp1)) put(getc(fp1, fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
- fseek函数
- 函数原型`int fseek(FILE *fp, long offset, int whence)`
- fp:文件指针
- offset:偏移量(以起始点为基点,移动的字节数),>0向后移动, <0向前移动
- whence:起始点
- SEEK_SET 0:文件开始
- SEEK_CUR 1:文件的当前位置
- SEEK_END 2:文件末尾
- 功能:改变文件位置指针的位置
- 返回值:成功,返回0,失败,返回非0值。
- ftell函数
- 函数原型:`long ftell(FILE *fp)`
- 功能:返回位置指针当前的位置(用相对文件开头的位移量表示)
- 返回值:成功:返回当前位置指针位置;失败,返回-1L
# I/O文件编程
- Linux系统调用
所谓系统调用是指操作系统提供给用户程序的一组“特殊的”接口,用户程序可以通过这个组“特殊”接口来获得操作系统内核提供的特殊服务。
在linux中用户程序不能直接访问内核提供的服务。为了更好的保护内核空间,将陈股的运行空间分为内核空间和用户空间,他们运行在不同的级别上,在逻辑上是相互隔离的。
有两种操作内核的方式:系统命令、用户程序接口(API)
- 用户编程接口(API)
在linux中用户编程接口(API)遵循了UNIX中最流行的应用编程界面标准——POSIX标准(可移植操作系统接口标准)。这些系统调用编程接口主要通过C库(libc)实现的。
- 文件I/O介绍
- 可用的文件I/O函数——打开文件、读文件、写文件等等。大多数linux文件I/O只需要5个函数:open、read、write、lseek、close
不带缓存指的是每个read和write都是调用内核中的一个系统调用。这些不带缓存的I/O函数不是ANSI C的组成部分,但是POSIX的组成部分。
- Linux文件描述符
对内核而言所有打开文件都有文件描述符引用。文件描述符是一个非负整数。当打开一个现存文件或创建一个新文件时,内核向进程返回一个文件描述符。当读、写一个文件时,用open或create返回的文件描述符标识该文件,将其作为参数传送给read或write。
在POSIX.1应用程序中,整数0、1、2应被代换成符号常数STDIN_FILENO、STDOUT_FILENO和STDERR_FILENO。这些常数都定义在头文件<unistd.h>中。
文件描述符的范围是0~OPEN_MAX。早期UNIX版本采用的上限是19(允许每个进程打开20个文件),现在很多程序则将其增加至63。
- POSIX文件编程
- open
include<sys/types.h>
include<sys/stat.h>
include<fcntl.h>
int open(const char *pathname, int oflag, mode_t mode);
- 返回:若成功返回文件描述符;若出错返回-1
- 参数
- pathname:需要打开的文件的路径和名称
- oflag:打开的标记,可用来说明此函数的多个选择项,参数如下(常数定义在<fcntl.h>)
- O_RDONLY:只读打开
- O_WRONLY:只写打开
- O_RDWR:读、写打开
- O_APPEND:每次写时,都是加到文件的尾端
- O_CREAT:若此文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权位
- O_EXCL:如果同时指定了O_CREAT,而文件已经存在,则出错。这可测试一个文件是否存在,如果不存在则创建此文件成为一个原子操作。
- O_TRUNC:如果此文件存在,而且为只读或只写成功打开,则将其长度截短为0(清空文件)
- O_NOCTTY:如果pathname指的是终端设备,则不将此设备分配作为此进程的控制终端
- O_NONBLOCK:如果pathname指的是一个FIFO、一个块特殊文件或一个字符特殊文件,则此选项为此文件打开操作和后续的I/O操作设置非阻塞方式。
- O_SYNC:是每次write都等到物理I/O操作完成。
- mode:打开的模式,对open函数而言,仅当创建新文件时才使用第三个参数。(文件权限chmod 777 filename)
mode标志用来表示文件的访问权限。
Linux总共用五个数字表示文件的权限(用户id、设置组id、自己权限、组权限、其他人权限)如果有O_CREAT参数时:`open("test", O_RDWR | O_CREAT, 10750);`,没有O_CREAT参数时:`open("test", O_RDWR); `
- creat
可用creat函数创建一个新文件
include<sys/stat.h>
include<sys/types.h>
include<fcntl.h>
int creat(const char* pathname, mode_t mode);
- 返回值:若成功返回只写打开的文件描述符;出错返回-1。
注意:此函数等效于:
`open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);`
- creat的一个不足之处是他以只写方式打开所创建的文件。
- close
include<unistd.h>
int close(int filedes);
- 返回值:若成功返回0;出错返回-1
- 当一个程序终止时,他所有的打开文件都由内核自动关闭。很多程序都是用这一功能而不显示地用close关闭打开的文件。
/open.c/
include<unistd.h>
include<sys/types.h>
include<sys/stat.h>
include<fcntl.h>
include<stdlib.h>
include<stdio.h>
int main()
{
int fd = -1;
fd = open("hello.c", O_CREAT |O_TRUNC | O_WRONLY, 00600);
if(fd < 0)
{
perror("open:");
exit(1);
}
else
printf("open file:1hello.c %d\n", fd);
if(close(fd) < 0){
perror("close");
exit(2);
}
else
printf("Close hello.c);
return 0;
}
- read
用read函数从打开文件中读取数据
include<unistd.h>
ssize_t read(int feledes, void* buff, size_t nbytes);
- 返回值:如果read成功,返回读到的字节数;若已到文件尾为0;出错返回-1;
有很多种情况可使实际读到的字节数少于要求读的字节数
1. 读普通文件时,在读到要求字节数之前已经到达了文件的尾端。例如,若在到达文件尾端之前还有30个字节,而要求读100个字节,则read返回30,下一次再调用read时,将会返回0(文件尾端)
- 当从终端设备读时,通常一次最多读一行。
- 当从网络读取时,网络中的缓冲机构可能造成返回值小于所要求读的字节数
- 某些面向记录的设备,例如磁带,一次做多返回一个记录
- 读操作从文件的当前位移量处开始,在成功返回之前,该位移量增加实际读取的字节数。
- write函数
用write函数向打开文件些数据
include<unistd.h>
ssize_t write(int filedes, const void* buff, size_t nbytes);
- 返回值:若成功为已写的字节数,若出错返回-1
- 其返回值通常与参数nbytes的值不同,则表示出错。write出错的一个常见原因是:磁盘已写满,或者超过了对一个给定进程的文件长度限制。
- 对于普通文件,写操作从稳健的当前位移量处开始。如果在打开该文件时,指定了O_APPEND选项,则在每次写操作之前,将文件位移量设置在文件的当前结尾处。再一次成功写之后,该文件位移量增加实际写的字节数。
/open.c/
include<unistd.h>
include<sys/types.h>
include<sys/stat.h>
include<fcntl.h>
include<stdlib.h>
include<stdio.h>
include<string.h>
int main()
{
int fd = -1;
int ret;
char buf[32];
fd = open("hello.c", O_CREAT | O_RDWD, 00600);
if(fd < 0)
{
perror("open:");
exit(1);
}
else
printf("open file:1hello.c %d\n", fd);
ret = read(fd, buf, 12);
if(ret < 0) printf("read error....\n");
else{
buf[ret] = '\0'; //手动添加字符串结束符
printf("str = %s\n", buf);
}
ret = write(fd, buf, strlen(buf));
if(ret == strlen(buf)) printf("write successly");
if(close(fd) < 0){
perror("close");
exit(2);
}
else
printf("Close hello.c);
return 0;
}
- lseek
include<sys/types.h>
include<unistd.h>
off_t lseek(int filesdes, off_t offset, int whence);
- 返回值:若成功为新的文件位移,若出错,返回-1。
- 对参数offset的解释与参数whence的值
- 若whence是SEEK_SET,则将该文件的位移量设置为局文件开始处offset个字节。
- 若whence是SEEK_CUR,则将该文件的偏移设置为其当前偏移量+offset,offset可以为正或负。
- 若whence是SEEK_END,则将该文件的位移量设置为文件长度+offset,offset可以为正或负。
若lseek成功执行,则返回新的文件位移量,为此可以用一下方式确定一个代开文件的昂前位移量:
off_t curr_pos;
curr_pos = lseek(fd, 0, SEEK_CUR);
include<sys/stat.h>
include<sys/types.h>
include<fcntl.h>
include<unistd.h>
include<string>
include<stdlib.h>
include<stdio.h>
int main()
{
int fd, size, len;
char *buf = "Hello! I'm writing to this file!";
char buf_r[10];
off_t curr;
len = strlen(buf);
if((fd = open("hello.c", O_CREAT | O_TRUNC | O_RDWR, 0666)) < 0)
{
perror("open:");
exit(1);
}
else printf("open file: hello.c %d\n", fd);
curr = lseek(fd, 0, SEEK_CUR);
printf("curr = %d\n", curr);
if((size = write(fd, buf, len)) < 0){
perror("write");
exit(2);
}
else printf("write: %s\n", buf);
curr = lseek(fd, 0, SEEK_CUR);
printf("curr = %d\n", curr);
lseek(fd, 0, SEEK_SET); //指针回到开头
if((size = read(fd, buf_r, 9)) < 0){ //从头读取9个字符
perro("read");
exit(3);
}
else{
buf_r[size] = '\0';
printf("read from file: %s, len = %d\n", buf_r, size);
}
if(close(fd) < 0){
perror("close");
exit(4);
}
else{
printf("close hello.c");
}
return 0;
}
操作多个终端的程序
include<stdlib.h>
include<stdio.h>
define TTY0 "/dev/pts/0" //终端设备名
define TTY1 "/dev/pts/1"
define TTY2 "/dev/pts/2"
define TTY3 "/dev/pts/3"
void tty_write(char* tty, char* buf)
{
int fd = -1;
fd = open(tty, O_RDWR);
if(fd < 0){
fprintf(stdin, "open tty err\n");
exit(0);
}
write(fd, buf, strlen(buf));
close(fd);
}
int main()
{
char buf[] = "test tty\n";
char tty[32];
strcpy(tty, TTY1);
tty_write(tty, buf);
strcpy(tty, TTY2);
tty_write(tty, buf);
return 0;
}
![消息发送的情况](http:https://img.haomeiwen.com/i5688965/dadfbe7d59408523.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![TTY2收到的消息](http:https://img.haomeiwen.com/i5688965/69047eecbce94d73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 总结:
1. 打开文件
`FILE *fopen(char *name, char *mode)`
- 文件关闭
`int fclose(FILE *fp);`
- 字符I/O:
`int fputc(int c, FILE* fp)`
`int fgetc(FILE* fp)`
- 数据块I/O:
`size_t fread(void* buffer, size_t size, size_t count, FILE *fp)`
`size_t fwrite(void* buffer, size_t size, size_t count, FILE *fp)`
- 格式化I/O:
`int fprintf(FILE *fp, const char* format[, argument,...])`
`int fscanf(FILE *fp, const char* format[, address...])`
- 字符串I/O:
`char* fgets(char *s, int n, FILE *fp)`
`int fput(char *s, FILE *fp)`
- rewind函数
`void rewind(FILE *fp)`
- fseek函数
`int fseek(FILE *fp, long offset, int whence)`
- open
include<sys/types.h>
include<sys/stat.h>
include<fcntl.h>
int open(const char *pathname, int oflag, mode_t mode);
- creat
include<sys/stat.h>
include<sys/types.h>
include<fcntl.h>
int creat(const char* pathname, mode_t mode);
- close
include<unistd.h>
int close(int filedes);
- read
include<unistd.h>
ssize_t read(int feledes, void* buff, size_t nbytes);
- write
include<unistd.h>
ssize_t write(int filedes, const void* buff, size_t nbytes);
- lseek
include<sys/types.h>
include<unistd.h>
off_t lseek(int filesdes, off_t offset, int whence);
网友评论