文件编程(main函数的带参的形式、实现cp指令、修改文件里面的数据、对文件写入一个整数、对文件写入一个结构体、写入结构体数组)
main()函数的带参的形式
int main(int argc,char** argv)
{
printf("total param:%d\n",argc);
printf("1:%s\n",argv[0]);
printf("2:%s\n",argv[1]);
printf("3:%s\n",argv[2]);
return 0;
}
运行结果:
实现cp指令
主体思路
- open 打开源文件
- 将原文件内容读 read 到 readBuf 中
- open 打开创建清零目标文件
- 将 readBuf 中的内容写入 write 目标文件
- close 关闭两个文件
代码
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc,char** argv)
{
if(argc!=3)
{
printf("The parram is not good!\n");
exit(-1);
}
1
char* readBuf=NULL;
int 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);
int fdDes=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
write(fdDes,readBuf,strlen(readBuf));
close(fdSrc);
close(fdDes);
return 0;
}
修改文件里面的数据
思路
1.打开文件
2.找到要修改关键词的位置
3.光标往后挪到要修改的位置(也可以直接找到修改的位置)
4.修改
比如我的文件里面有
SPEED=2
LENGTH=8
LEVEL=6
DD=11
我想修改LENGTH的内容 让LENGTH=8。
-
打开文件
-
找到LENGTH=
-
让光标(指针)再往后移动到=后面。
-
修改数据。(让光标回到文件头)
-
写进文件
-
关闭文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc,char** argv)
{
if(argc!=2)
{
printf("The param is not good!\n");
exit(-1);
}
int fdSrc;
char* readBuf=NULL;
fdSrc=open(argv[1],O_RDWR);
int size=lseek(fdSrc,0,SEEK_END);
printf("size=%d\n",size);
lseek(fdSrc,0,SEEK_SET);
readBuf=(char*)malloc(sizeof(char)*size+8);
memset(readBuf,0,sizeof(readBuf));
int n_read=read(fdSrc,readBuf,size);
printf("----------------------n_read=%d-------------------------\n",n_read);
char* p=strstr(readBuf,"LENGTH=");
if(p==NULL)
{
printf("not found\n");
exit(-1);
}
p=p+strlen("LENGTH=");
*p='8';
lseek(fdSrc,0,SEEK_SET);
write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
return 0;
}
原配置文件
修改后的配置文件
对文件写入一个整数
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd=0;
int data1=8;
int data2;
fd=open("./file",O_RDWR);
int n_write=write(fd,&data1,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 <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd=0;
int data1[2]={8,9};
int data2[2];
fd=open("./file",O_RDWR);
int n_write=write(fd,&data1,sizeof(int)*2);
lseek(fd,0,SEEK_SET);
int n_read=read(fd,&data2,sizeof(int)*2);
printf("read %d %d\n",data2[0],data2[1]);
close(fd);
return 0;
}
对文件写入一个结构体
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
struct Test
{
int a;
char c;
};
int main()
{
struct Test data1={100,'a'};
struct Test data2;
int fd;
fd=open("./file",O_RDWR);
int n_write=write(fd,&data1,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 <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
struct Test
{
int a;
char c;
};
int main()
{
int fd;
struct Test data1[2]={{100,'a'},{99,'b'}};
struct Test data2[2];
fd=open("./file",O_RDWR);
int n_write=write(fd,&data1,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;
}