1.打开文件之 Open 操作
// int open(const char *pathname, int flags);
// int open(const char *pathname, int flags, mode_t mode);
两个open函数区别在于是否有权限。
所需要的头文件有
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
open操作
int main()
{
int fd;
fd=open("./file1",O_RDWR);
printf("%d\n",fd);
return 0;
}
// 如果文件不存在,fd返回值为 -1
//为了避免 文件不存在 fd返回值为-1的情况,然后使用另一种open函数
int main()
{
int fd;
fd=open("./file1",O_RDWR);
printf("%d\n",fd);
if(fd==-1)
{
printf("This file1 open failed");
}
fd=open("./file1",O_RDWR|O_CREAT,0600);
return 0;
}
Flags:
O_RDONLY 只读打开
O_WRONLY 只写打开
O_RDWR 可读可写打开
打开文件的权限有以下几种:
O_CREAT 若文件不存在,则创建它 使用该权限时,需要同时说明第三个参数mode 说明该文件的存取许可
O_EXCL 如果同时指定了 O_CREAT 而文件已经存在 则出错
O_APPEND 每次写的时候 都加到文件的尾端
O_TRUNC 属性打开文件时,如果这个文件本来是有内容的,而且为只读或只写成功打开,则其长度截短为0可以根据不同的情况进行使用。
2.写入文件之Write操作
// ssize_t write(int fd, const void *buf, size_t count);
三个参数分别代表:
1.文件符
2.目标位置
3.需要写入的内容大小
//Sizeof 测 指针的大小 因为在Linux下,指针大小为8字节
//Strlen 测 字符串的大小
#include <sys/types.>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
Write操作
int main()
{
int fd;
char *buf="wang jia hao";
fd=open("./file1",O_RDWR);
printf("%d\n",fd);
if(fd==-1)
{
printf("This file1 open failed\n");
fd=open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0)
{
printf("Creat File1 Successful\n");
}
}
printf("Opne file Successful! fd:%d\n",fd);
// ssize_t write(int fd, const void *buf, size_t count);
//Sizeof 测 指针的大小 因为在Linux下,指针大小为8字节
//Strlen 测 字符串的大小
write(fd,buf,strlen(buf));
return 0;
}
3.读取文件之Read操作
// ssize_t read(int fd, void *buf, size_t count);
三个参数分别代表:
1.文件符
2.目标位置
3.需要读取的内容大小
所需要的头文件有:
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>在进行读取文件操作之前,需要注意光标的位置,如果光标在末尾,则不会读取任何内容
。需要把光标进行前移操作才可以进行读取操作。
int main()
{
int fd;
char *buf="wang jia hao";
fd=open("./file1",O_RDWR);
printf("%d\n",fd);
if(fd==-1)
{
printf("This file1 open failed\n");
fd=open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0)
{
printf("Creat File1 Successful\n");
}
}
printf("Opne file Successful! fd:%d\n",fd);
int n_write=write(fd,buf,strlen(buf));
if(n_write!=-1)
{
printf("Write %d byte to file!\n",n_write);
}
close(fd);
fd=open("./file1",O_RDWR);
char * Read_Buf;
Read_Buf=(char *)malloc(sizeof(char)*n_write+1);
int n_read=read(fd,Read_Buf,n_write);
printf("Read %d , context %s \n",n_read,Read_Buf);
return 0;
}
以上代码通过写入完文件操作之后,关闭文件,然后重新打开,从而让光标进行移动,方法比较笨重,为了进行优化,可以通过lseek函数进行对光标的操作。
// Lseek 函数
off_t lseek(int fd, off_t offset, int whence);whence:
SEEK_SET: 指向文件的头
SEEK_CUR:指向当前光标位置
SEEK_END:指向文件的尾offset:
对 whence的偏移值。/* 调用Lseek函数(三种方法)
lseek(fd,0,SEEK_SET);lseek(fd,-12,SEEK_CUR);
正数:向末尾偏移
负数:向前面偏移
lseek(fd,-12,SEEK_END);
*/通过lseek对光标进行操作
int main()
{
int fd;
char *buf="wang jia hao";
fd=open("./file1",O_RDWR);
printf("%d\n",fd);
if(fd==-1)
{
printf("This file1 open failed\n");
fd=open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0)
{
printf("Creat File1 Successful\n");
}
}
printf("Opne file Successful! fd:%d\n",fd);
int n_write=write(fd,buf,strlen(buf));
if(n_write!=-1)
{
printf("Write %d byte to file!\n",n_write);
}
lseek(fd,0,SEEK_SET);
char * Read_Buf;
Read_Buf=(char *)malloc(sizeof(char)*n_write+1);
int n_read=read(fd,Read_Buf,n_write);
printf("Read %d , context %s \n",n_read,Read_Buf);
return 0;
}
sleek函数的妙用,可以用来进行计算内容的长度。
int size = lseek(fd,0,SEEK_END);
4.对cp操作进行改写
参数:
argc 参数个数
argv 参数名CP src.c des.c
argv[0],argv[1],argv[2];
argc //表示参数个数思路:
1.打开src.c
2.读取src到buf
3.打开/创建des.c
4.将buf写入到des.c
5.close两个文件
所用到的头文件
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
My cp操作
int main(int argc,char**argv)
{
int fdSrc;
int fdDes;
char *readBuf=NULL;
if(argc!=3)
{
printf("Pararm error!\n");
exit(-1);
}
fdSrc= open(argv[1],O_RDWR);
int size=lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
readBuf=(char*)malloc(sizeof(char)*size+5);
int n_read= read(fdSrc,readBuf,1024);
fdDes=open(argv[2],O_RDWR|O_CREAT,0600);
int n_Write= write(fdDes,readBuf,strlen(readBuf));
close(fdSrc);
close(fdDes);
return 0;
}
5.文件操作之修改配置文件
修改配置文件:
1.找到需要修改的字符的位置
2.通过指针偏移字符,找到需要修改的数据的位置
3.修改
所用到的头文件有:
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
该文件里的数据如下:
Size=10;
Long=9;
Leng=5;
Color=2;
目标 为 修改Leng的结果;
int main(int argc,char**argv)
{
int fdSrc;
char *readBuf=NULL;
if(argc!=2)
{
printf("Pararm error!\n");
exit(-1);
}
fdSrc= open(argv[1],O_RDWR);
int size=lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
readBuf=(char*)malloc(sizeof(char)*size+5);
int n_read= read(fdSrc,readBuf,size);
char *p=strstr(readBuf,"Leng=");
if(p==NULL)
{
printf("Not Found \n");
exit(-1);
}
p=p+strlen("leng=");
*p = '7';
lseek(fdSrc,0,SEEK_SET);
int n_Write= write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
return 0;
}
修改后的配置文件内容为:
Size=10;
Long=9;
Leng=7;
Color=2;
本文详细介绍了C语言中的文件操作,包括open函数的区别、write和read操作的参数解析,以及如何使用lseek优化光标位置。还展示了如何使用lseek进行文件复制和修改配置文件中的特定数据。
1512





