实现Linux cp命令
./mycp 1.txt 2.txt
讲1.txt的文件内容复制到2.txt
如果2.txt存在清空内容并重新写入,第二个参数必须指定路径+名字或者指定名字,则原来的文件叫什么复制过来的文件名就叫什么
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
//off_t lseek(int fd, off_t offset, int whence);
//int open(const char *pathname, int flags, mode_t mode);
//ssize_t read(int fd, void *buf, size_t count);
//ssize_t write(int fd, const void *buf, size_t count);
#define my_assert(x) if(x<=0){perror("why");exit(-1);}
int main(int argc,char **argv)
{
int fdsrc = 0;
int fddes = 0;
char *readbuf = 0;
char *writebuf = 0;
if(argc!=3){
printf("param error\n");
exit(-1);
}
//打开文件
fdsrc = open(argv[1],O_RDWR);
my_assert(fdsrc);
printf("file open success\n");
//计算文件大小
int f_size = lseek(fdsrc,0,SEEK_END);
readbuf = (char *)malloc(sizeof(char)*f_size+2);
my_assert(readbuf);
//读文件
lseek(fdsrc,0,SEEK_SET);
int rd_ret=read(fdsrc,readbuf,f_size);
my_assert(rd_ret);
printf("read %d byte,context:%s\n",rd_ret,readbuf);
close(fdsrc);
//打开文件 权限改为可读可写
fddes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,00600);
my_assert(fddes);
int wd_rte = write(fddes,readbuf,f_size);
my_assert(fddes);
printf("write %d byte,context:%s\n",f_size,readbuf);
close(fddes);
}
配置文件修改
test.config文件内容如下:
我这里提供一个比较完整的配置文件修改程序,但是比较复杂,如果实在看不懂可以看下面那个比较简单的demo程序
配置文件按照我这个格式就不会出现问题,注意每个属性后面一定要加空格
HP=12
MONEY=100
AP=50
SPEED=30
LOCATION=12000
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
//off_t lseek(int fd, off_t offset, int whence);
//int open(const char *pathname, int flags, mode_t mode);
//ssize_t read(int fd, void *buf, size_t count);
//ssize_t write(int fd, const void *buf, size_t count);
#define my_assert(x) if(x<=0){perror("why");exit(-1);}
int main(int argc,char **argv)
{
int fdsrc = 0;
int fddes = 0;
char *readbuf = 0;
char *writebuf = 0;
int cnt = 0;
if(argc!=4){
printf("Usage: %s [file] [property] [value]\n", argv[0]);
exit(-1);
}
char *filename = argv[1];
char *property = argv[2];
char *value = argv[3];
char modify_buf[128];
sprintf(modify_buf,"%s=%s\n",property,value);
// printf("Mfy_buf:%s\n",modify_buf);
//打开文件
fdsrc = open(filename,O_RDWR);
my_assert(fdsrc);
//计算文件大小
int f_size = lseek(fdsrc,0,SEEK_END);
readbuf = (char *)malloc(sizeof(char)*f_size+2);
my_assert(readbuf);
//读文件
lseek(fdsrc,0,SEEK_SET);
int rd_ret=read(fdsrc,readbuf,f_size);
my_assert(rd_ret);
// printf("read %d byte,context:\n%s\n",rd_ret,readbuf);
close(fdsrc);
char *pos=strstr(readbuf, property);
if(pos==NULL){
printf("not found\n");
exit(-1);
}
char probuf[128]={0};
char *pro_pos = probuf;
if(readbuf==pos){ //需要修改首行的值
memset(probuf,0,sizeof(probuf));
}
else
{
while(readbuf!=pos){ //判断地址是否相等 拷贝前部分内容
*pro_pos = *readbuf;
readbuf++;
pro_pos++;
}
}
// printf("read %d byte,context:\n%s\n",strlen(probuf),probuf);
char Taibuf[128]={0};
char *Tai_pos = Taibuf;
//拷贝后部分内容
while(*readbuf!='\n'){
readbuf++;
}
readbuf++;
if(*readbuf==NULL){ //这是修改最尾部内容
memset(Taibuf,0,sizeof(Taibuf));
}
else{
while(*readbuf!=0){ //判断地址是否相等
*Tai_pos = *readbuf;
readbuf++;
Tai_pos++;
}
}
// printf("read %d byte,context:\n%s\n",strlen(Taibuf),Taibuf);
char *fin_buf = strcat(probuf,modify_buf);
fin_buf = strcat(fin_buf,Taibuf);
fdsrc = open(filename,O_RDWR|O_TRUNC);
my_assert(fdsrc);
int wd_rte = write(fdsrc,fin_buf,strlen(fin_buf));
my_assert(wd_rte);
printf("write %d byte,context:\n%s\n",f_size,fin_buf);
close(fdsrc);
}
这下面的话是一个简单的程序修改一位数
SPEED=3
LENG=3
SCORE=3
LEVEL=5
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
int fdsrc;
char *readbuf;
if(argc!=2){
printf("param 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;
}
写一个整数到文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
int data = 100;
int data2 = 0;
fd = open("./file",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 <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct Test
{
int a;
char c;
/* data */
};
int main()
{
int fd;
struct Test data={100,'a'};
struct Test data2;
fd = open("./file",O_RDWR);
int n_write = write(fd,&data,sizeof(data));
lseek(fd,0,SEEK_SET);
int n_read = read(fd,&data2,sizeof(data));
printf("read %d %c\n",data2.a,data2.c);
close(fd);
return 0;
}
写一个结构体数组到文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct Test
{
int a;
char c;
/* data */
};
int main()
{
int fd;
struct Test data[2]={{100,'a'},{101,'b'}};
struct Test data2[2];
fd = open("./file",O_RDWR);
int n_write = write(fd,&data,sizeof(data));
lseek(fd,0,SEEK_SET);
int n_read = read(fd,&data2,sizeof(data));
printf("read %d %c\n%d %c",data2[0].a,data2[0].c,data2[1].a,data2[1].c);
close(fd);
return 0;
}