一、open函数的细节补充
1、 O_EXCL 如果同时指定了O_CREAT,而文件已经存在,则返回-1;
fd= open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
if(fd == -1){
printf("fd = %d,file alrealy exists\n",fd);
return 0;
}
2、O_APPEND 每次写时都加到文件尾端;
fd= open("./file1",O_RDWR|O_APPEND);
3、O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或者
只写成功打开,则将其长度截短为0。
#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;
char *buf="test";
fd= open("./file1",O_RDWR|O_TRUNC);
printf("open success:fd=%d\n",fd);
int n_write= write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d bytes to file\n",n_write);
close(fd);
return 0;
}
二、creat函数使用
函数原型和头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char *pathname, mode_t mode);
fd= creat("/home//file1",S_IRWXU); //参数1:路径名 参数2:文件类型
S_IRWXU可读,可写,可执行
三、编写CP指令
参照Linux命令终端下的cp指令,自主编写一个自己拷贝命令代码,
需要涉及到main函数的参数问题,用到 int main(int argc,char **argv) //argc 为参数个数 argv为二级指针(字符指针数组)
代码实现
#include <string.h>
#include <stdlib.h>
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+8);
int n_read=read(fdSrc,readBuf,size);
fdDes=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
int n_write=write(fdDes,readBuf,strlen(readBuf));
close(fdSrc);
close(fdDes);
return 0;
}
编译方式
./a.out demo.c new.c
这里argv为demo.c,argv[2]即为new.c
四、修改配置文件
需要用到strstr函数,找到对应要修改的信息位置,并且指针开始指向被查找的字符串开头
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+8);
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='5';
lseek(fdSrc,0,SEEK_SET);
int n_write=write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
return 0;
}
编译格式:./a.out TEST.config
五、写整形数或结构体到文件
写整形数到文件
#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);
printf("open success:fd=%d\n",fd);
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[2]={{100,'a'},{101,'b'}};
struct Test data2;
fd= open("./file1",O_RDWR);
printf("open success:fd=%d\n",fd);
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",data[0].a,data[0].c);
printf("read %d,%c\n",data[1].a,data[1].c);
close(fd);
return 0;
}