文件编程练习

自己实现linux CP指令

实现cp指令的思路:

  • 打开要复制的原文件
  • 读原文件的内容到buf
  • 打开或者创建要粘贴的文件
  • 将buf里面的内容写到目标文件
  • 关闭两个文件

main 函数的标准原型:

main 函数的标准原型应该是 int main(int argc, char *argv[]);argc 是命令行参数的个数。而 argv 是一个指向指针的指针,为什么不是指针数组呢?因为前面讲过,函数原型中的[]表示指针而不表示数组,等价于 char **argv 。那为什么要写成 char *argv[] 而不写成 char **argv 呢?这样写给读代码的人提供了有用信息,argv 不是指向单个指针,而是指向一个指针数组的首元素。数组中每个元素都是 char * 指针,指向一个命令行参数字符串。

demo:

#include<stdio.h>
int main(int argc,char *argv[])//argc表示参数的个数,argv表示字符串数组是二级指>针
{printf("参数总个数是:%d\n",argc);printf("第一个参数是:%s\n",argv[0]);printf("第二个参数是:%s\n",argv[1]);printf("第三个参数是:%s\n",argv[2]);return 0;
}程序运行的结果:
fhn@ubuntu:~/linuxfile$ ./cpfile src des
参数总个数是:3
第一个参数是:./cpfile
第二个参数是:src
第三个参数是:des

实现cp指令:

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdlib.h>
#include <unistd.h>
#include<string.h>
int main(int argc,char *argv[])//argc表示参数的个数,argv表示字符串数组是二级指针
{int fd;int fd2;int size;int n_read;int n_write;char*readbuf=NULL;if(argc!=3){printf("输入参数个数有误\n");exit(0);//正常退出              }fd=open(argv[1],O_RDONLY);if(fd==-1){printf("文件打开错误\n");perror("open");exit(0);}size=lseek(fd,0,SEEK_END);readbuf=(char*)malloc(sizeof(char)*size+8);lseek(fd,0,SEEK_SET);n_read=read(fd,readbuf,sizeof(char)*size+8);if(n_read==-1){printf("文件读取错误\n");perror("read");exit(0);}fd2=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);if(fd2==-1){printf("目标文件打开失败\n");perror("open");exit(0);}n_write=write(fd2,readbuf,strlen(readbuf));if(n_write==-1){printf("文件写入失败");perror("write");exit(0);}close(fd);close(fd2);return 0;
}

配置文件的修改:

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdlib.h>
#include <unistd.h>
#include<string.h>
int main(int argc,char *argv[])//argc表示参数的个数,argv表示字符串数组是二级指针
{int fd;int size;int n_read;int n_write;char* find=NULL;char* readbuf=NULL;if(argc!=2){printf("输入参数个数有误\n");exit(0);//正常退出              }fd=open(argv[1],O_RDWR);if(fd==-1){printf("文件打开错误\n");perror("open");exit(0);}size=lseek(fd,0,SEEK_END);lseek(fd,0,SEEK_SET);readbuf=(char*)malloc(sizeof(char)*size);n_read=read(fd,readbuf,size*sizeof(char));if(n_read==-1){printf("文件读取错误\n");close(fd);perror("read");exit(0);}find=strstr(readbuf,"heigh=");if(find==NULL){printf("配置文件中没有要修改的内容\n");close(fd);exit(0);}find=find+strlen("heigh=");*find='1';*(++find)='8';*(++find)='0';lseek(fd,0,SEEK_SET);n_write=write(fd,readbuf,strlen(readbuf));if(n_write==-1){printf("写入失败\n");close(fd);perror("write");exit(0);}close(fd);return 0;
}

写一个整数到文件:

#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main()
{int fd;int a=110;int b=0;fd=open("./test.config",O_RDWR|O_APPEND);write(fd,&a,sizeof(int));lseek(fd,-4,SEEK_END);//因为写入的是整型,所以光标要移动4个字节read(fd,&b,sizeof(int));printf("写入的是:%d\n",b);close(fd);return 0;
}#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main()
{int fd;short int a=110;int b=0;fd=open("./test.config",O_RDWR|O_APPEND);write(fd,&a,sizeof(short int));lseek(fd,-2,SEEK_END);read(fd,&b,sizeof(int));printf("写入的是:%d\n",b);close(fd);return 0;
}

写结构体到文件:

#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct Test
{int a;char b;
};
int main()
{int fd;struct Test Data1={1,'a'};struct Test Data2;fd=open("./test.config",O_RDWR|O_APPEND);write(fd,&Data1,sizeof(struct Test));lseek(fd,-8,SEEK_END);read(fd,&Data2,sizeof(struct Test));printf("结构体大小是:%d\n",(int)sizeof(struct Test));printf("写入的是a=%d,b=%c\n",Data2.a,Data2.b);close(fd);return 0;
}

写结构体数组到文件:

#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct Test
{int a;char b;
};
int main()
{int fd;struct Test Data1[2]={{1,'a'},{2,'b'}};struct Test Data2[2];fd=open("./test.config",O_RDWR|O_APPEND);write(fd,&Data1,sizeof(struct Test)*2);lseek(fd,-16,SEEK_END);read(fd,&Data2,sizeof(struct Test)*2);printf("写入的是a=%d,b=%c\n",Data2[0].a,Data2[0].b);printf("写入的是a=%d,b=%c\n",Data2[1].a,Data2[1].b);close(fd);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/272233.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java.lang.OutOfMemoryError: GC overhead limit exceeded

今天现场weblogic报java.lang.OutOfMemoryError: GC overhead limit exceeded&#xff0c;在metalink查了下&#xff0c;有明白解释&#xff0c;要设置一个JVM參数。只是因为当前weblogic内存设置为4G&#xff0c;所以设置參数的做法事实上并非解决这个问题之道。还是要分析web…

[翻译] Visual Studio 2019 RC版发布

今天&#xff0c;我们将分享 Visual Studio 2019 的发布候选版(RC 版) - 这是在 4 月 2 日的虚拟发布活动上正式发布之前的最后步骤之一。 您可以在 visualstudio.com/downloads 下载 RC 版。与往常一样&#xff0c;查看RC 版的发行说明以获取更多详细信息。发布候选版的说明在…

fread、fwrite、fopen函数的简单使用和open、read、write区别解析

这几个函数的区别&#xff1a;fread、fwrite、fopen和open、read、write区别解析 标准C库函数的简单使用 fopen函数原型&#xff1a; #include <stdio.h> FILE *fopen(const char *pathname, const char *mode);第一个参数是&#xff1a;要打开的文件路径 第二个参数是…

docker安装rocketmq你学会了吗

防火墙开通端口 9876 10911 9800 firewall-cmd --zonepublic --add-port9876/tcp --permanent firewall-cmd --zonepublic --add-port10911/tcp --permanent firewall-cmd --zonepublic --add-port9800/tcp --permanent firewall-cmd --reload 创建存储文件夹 mkdir -p /root…

程序员的编程能力与编程年龄

作者丨酷壳/陈皓&#xff0c; http://coolshell.cn/articles/10688.html程序员这个职业究竟可以干多少年&#xff0c;在中国这片神奇的土地上&#xff0c;很多人都说只能干到30岁&#xff0c;然后就需要转型&#xff0c;就像《程序员技术练级攻略》这篇文章很多人回复到这种玩…

Rocketmq集群架构图

集群架构图 集群特点

进程相关概念、C程序的空间分配

进程的定义&#xff1a; “进程”是操作系统的最基本、最重要的概念之一。但迄今为止对这一概念还没有一个确切的统一的描述。下面给出几种对进程的定义描述。 进程是程序的一次执行。进程是可以并行执行的计算。进程是一个程序与其使用的数据在处理机上顺序执行时发生的活动。…

(精)C#中TransactionScope的使用方法和原理

标签&#xff1a;.net transactionscope原创作品&#xff0c;允许转载&#xff0c;转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://cnn237111.blog.51cto.com/2359144/1271600在.net 1.1的时代&#xff0c;还没有TransactionScope…

一文搞定并发面试题

1、Object 的 wait()和notify() 方法下图为线程状态的图&#xff1a;Object 对象中的 wait()和notify()是用来实现实现等待 / 通知模式。其中等待状态和阻塞状态是不同的。等待状态的线程可以通过notify() 方法唤醒并继续执行&#xff0c;而阻塞状态的线程则是等待获取新的锁。…

fork、vfork、wait、waitpid

fork函数&#xff1a; 一个进程&#xff0c;包括代码、数据和分配给进程的资源。fork&#xff08;&#xff09;函数通过系统调用创建一个与原来进程几乎完全相同的进程&#xff0c;也就是两个进程可以做完全相同的事&#xff0c;但如果初始参数或者传入的变量不同&#xff0c;两…

java解析xml

<?xml version"1.0" encoding"UTF-8"?> <mimetype><default><mime-type>text/html</mime-type></default><mime-mapping><extension>zip</extension><mime-type>application/zip</mime-…

Linux常见英文报错中文翻译(菜鸟必知)

Linux常见英文报错中文翻译(菜鸟必知)1.command not found 命令没有找到2.No such file or directory 没有这个文件或目录3.Permission denied 权限不足4.No space left on device 磁盘没有剩余空间5.File exists 文件已经存在6.Is a directory 这是1个目录7.Not a directory 不…

阿里开源分布式事务seata带你入门

介绍 Seata 是阿里巴巴开源的分布式事务中间件&#xff0c;一种分布式事务解决方案&#xff0c;具有高性能和易于使用的微服务架构。 1:对业务无侵入&#xff1a;即减少技术架构上的微服务化所带来的分布式事务问题对业务的侵入 2:高性能&#xff1a;减少分布式事务解决方案…

exec族函数、system函数、popen函数、PATH

exec族函数函数的作用&#xff1a; 我们用fork函数创建新进程后&#xff0c;经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时&#xff0c;该进程被完全替换为新程序&#xff08;在exec都后面的代码不会被得到执行&#xff09;。因为调用exec函数并不创建…

jquery.validate.unobtrusive的使用

应用 一、引入 <script src"Scripts/jquery-1.7.1.min.js"></script> <script src"Scripts/jquery.validate.js"></script> <script src"Scripts/jquery.validate.unobtrusive.js"></script> 二、1&#xf…

Linux操作系统六大优点

❤️作者主页&#xff1a;IT技术分享社区 ❤️作者简介&#xff1a;大家好,我是IT技术分享社区的博主&#xff0c;从事C#、Java开发九年&#xff0c;对数据库、C#、Java、前端、运维、电脑技巧等经验丰富。 ❤️个人荣誉&#xff1a; 数据库领域优质创作者&#x1f3c6;&#x…

[webview] 放大缩小的问题

http://www.cocoachina.com/bbs/read.php?tid33249转载于:https://www.cnblogs.com/zxykit/p/5274831.html