师承------陈立臣
文章目录
文件的类型
1.普通文件
-
2.目录文件
d
3.字符设备文件
c
4.块设备文件
b
5.管道文件
f
6.链接文件
l
7.socket文件
s
每个文件分为:索引节点(inode)
和文件数据块
索引节点:记录文件的各种属性信息
文件的数据块:保存文件的具体内容
一、文件操作
打开/创建文件
关闭文件
文件定位
读文件
写文件
二、自行实现cp命令
思路:
1.打开源文件
2.读出源文件的数据
3.打开目标文件
4.向目标文件写入数据
5.关闭文件描述符
代码如下(示例):
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
int fd;
int fd1;
int filesize;
char* readbuf=NULL;
if(argc<3){
printf("the parm is no corect!\n");
return -1;
}
fd=open(argv[1],O_RDWR);
if(fd<0){
printf("open %s failed!\n",argv[1]);
}
filesize=lseek(fd,0,SEEK_END);
readbuf=(char*)malloc(sizeof(char)*filesize+5);
lseek(fd,0,SEEK_SET);
read(fd,readbuf,filesize);
fd1=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
if(fd1<0){
printf("open %s failed!\n",argv[1]);
return -1;
}
int n_write=write(fd1,readbuf,filesize);
printf("write ok the byte is %d\n",n_write);
close(fd1);
close(fd);
return 0;
}
巧用lseek计算文件大小
代码如下(示例):
lseek(fd,0,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;
int data = 100;
int data2 = 0;
fd = open("./file1",O_RDWR);
int n_write = write(fd,&data,sizeof(int));
lseek(fd,0,SEEK_SET);
int n_read = read(fd, &data2, sizeof(int));
printf("read %d \n",data2);
close(fd);
return 0;
}
四、如何写一个结构体或者结构体数组到文件?
写入结构体到文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct Test
{
int a;
char c;
};
int main()
{
int fd;
struct Test data = {100,'a'};
struct Test data2;
fd = open("./file1",O_RDWR);
int n_write = write(fd,&data,sizeof(struct Test));
lseek(fd,0,SEEK_SET);
int n_read = read(fd, &data2, sizeof(struct Test));
printf("read %d,%c \n",data2.a,data2.c);
close(fd);
return 0;
}
写入结构体数组到文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct Test
{
int a;
char c;
};
int main()
{
int fd;
struct Test data[2] = {{100,'a'},{101,'b'}};
struct Test data2[2];
fd = open("./file1",O_RDWR);
int n_write = write(fd,&data,sizeof(struct Test)*2);
lseek(fd,0,SEEK_SET);
int n_read = read(fd, &data2, sizeof(struct Test)*2);
printf("read %d,%c \n",data2[0].a,data2[0].c);
printf("read %d,%c \n",data2[1].a,data2[1].c);
close(fd);
return 0;
}
五、标准c库的打开/关闭/文件定位/写文件/读文件
六、标准c库对文件的操作示例
#include <stdio.h>
#include <string.h>
int main()
{
//FILE *fopen(const char *path, const char *mode);
FILE *fp;
char *str = " hen shuai";
char readBuf[128] = {0};
fp = fopen("./file.txt","w+");
//size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
//ptr buf
//size sizeof char
// geshu
// which file
fwrite(str,sizeof(char),strlen(str),fp);
// fwrite(str,sizeof(char)*strlen(str),1,fp);
fseek(fp,0,SEEK_SET);
// size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
fread(readBuf,sizeof(char),strlen(str),fp);
printf("read data: %s\n",readBuf);
return 0;
}
fgetc feof的使用
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
int i;
char c;
fp = fopen("./test.txt","r");
while(!feof(fp)){// nonezero if reach end of file
c = fgetc(fp);
printf("%c",c);
}
fclose(fp);
return 0;
}
七、fopen与open的区别
1.来源
open
是Linux系统调用的函数,返回文件描述符
fopen
是标准c库,返回的是文件结构体指针
2.移植性
fopen
来源于标准c库,移植性很强,而open
只能在Linux系统下使用,移植性稍差
3.缓冲
open无缓冲,fopen有缓冲。前者与read, write等配合使用, 后者与fread,fwrite等配合使用
点此博文,了解更加详细