linux文件编程(3)—— main函数传参、myCp(配置成环境变量)、修改配置文件、整数和结构体数组写到文件
參考:linux文件編程(3)—— 文件編程的簡單應用:myCp、修改配置文件
作者:丶PURSUING
發布時間: 2021-04-09 23:45:05
網址:https://blog.csdn.net/weixin_44742824/article/details/115209404
部分參考:https://leo-max.blog.csdn.net/article/details/112966334
目錄
- 實現cp指令
- 掌握向main函數傳參
- cp指令實現思路
- 實現代碼
- myCp指令全局可用
- 修改配置文件
- 整數寫入文件
- 結構體數組寫到文件
實現cp指令
掌握向main函數傳參
int main(int argc,char** argv )/** 參數 argc: 參數個數 **argv:二級指針,數組的指針(argv[0]、argv[1]、argv[2]...),即這個指針里的每一項都是一個數組(字符串) **/最直觀的還是例子
#include <stdio.h>int main(int argc,char** argv) {printf("參數個數:%d\n",argc);printf("argv[0]:%s\n",argv[0]);printf("argv[1]:%s\n",argv[1]);printf("argv[2]:%s\n",argv[2]);return 0; }運行結果:
cp指令實現思路
(1)打開src源文件(要被復制的文件)
(2)把源文件的內容讀入buf
(3)創建目標文件
(4)把buf內容寫入目標文件
(5)關閉源文件與目標文件
實現代碼
(1)精簡不帶調試版
#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) {//(1)打開src源文件(要被復制的文件)int fdSrc = open(argv[1],O_RDWR);int sizeOfSrc = lseek(fdSrc,0,SEEK_END);lseek(fdSrc,0,SEEK_SET);//(2)把源文件的內容讀入bufchar* readBuf = (char* )malloc(sizeOfSrc);memset(readBuf,'\0',sizeOfSrc);int retRead = read(fdSrc,readBuf,sizeOfSrc);//(3)創建目標文件,如果已經存在則覆蓋 //O_TRUN原文件度先截為0int fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);//(4)把buf內容寫入目標文件int retWrite = write(fdDes,readBuf,sizeOfSrc);//(5)關閉源文件與目標文件close(fdSrc);close(fdDes);return 0; }(2)打印調試信息對open,read,write的狀態進行跟蹤版
#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) {if(argc != 3){printf("參數個數錯誤\n");exit(-1);}//(1)打開src源文件(要被復制的文件)int fdSrc = open(argv[1],O_RDWR);if(fdSrc < 0){printf("open src error\n");}int sizeOfSrc = lseek(fdSrc,0,SEEK_END);lseek(fdSrc,0,SEEK_SET);char* readBuf = (char* )malloc(sizeOfSrc);memset(readBuf,'\0',sizeOfSrc);//(2)把源文件的內容讀入bufint retRead = read(fdSrc,readBuf,sizeOfSrc);if(retRead < 0){printf("read error\n");exit(-1);}//(3)創建目標文件,如果已經存在則覆蓋 //O_TRUN原文件長度截為0int fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);if(fdDes < 0){printf("open Des error\n");exit(-1);}//(4)把buf內容寫入目標文件int retWrite = write(fdDes,readBuf,sizeOfSrc);if(retWrite < 0){printf("write error\n");exit(-1);}//(5)關閉源文件與目標文件close(fdSrc);close(fdDes);return 0; }運行結果:生成了和mainPro.c 一樣的new.c
myCp指令全局可用
(1)查看當前所配置的環境變量
echo $PATH發現并沒有myCp可執行文件的路徑,所以要手動添加進去。
在此之前,還可以創建一個文件夾,專門用來存放我們自己寫的命令,如myCp,myLs等等,方便全局使用,而不僅僅限于所在路徑下使用了。使用方法直接myCp XX XX(不必再使用./myCp XX XX這種方法了)。
(2)創建文件夾,把myCp放進去
mkdir MYPATH(3)pwd查看當前路徑為:
/home/zhua/wenJian/MYPATH- 1
(4)添加路徑:
export PATH=$PATH:/home/zhua/wenJian/MYPATH其中$PATH也可替換成上面圖片所顯示的環境變量,即:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:
(5)再次查看配置的環境變量:配置成功(在最后尾巴上,此時只要在這個文件夾下的可執行命令都可以在全局運行)
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/zhua/wenJian/MYPATH修改配置文件
現有某配置文件如config,要求利用文件編程把length的值修改為9.
文件運行參數: score=90 length=5 high=10簡單示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>int main() {//(1)打開配置文件int fdConfig = open("./config",O_RDWR);//(2)計算配置文件大小int sizeOfConfig = lseek(fdConfig,0,SEEK_END);lseek(fdConfig,0,SEEK_SET);char* readBuf = (char* )malloc(sizeOfConfig);memset(readBuf,'\0',sizeOfConfig);//(3)讀取文件內容到readBufread(fdConfig,readBuf,sizeOfConfig);//(4)找到并修改length=后面的內容char* p = strstr(readBuf,"length=");p = p + strlen("length=");*p = '9';//(5)重新將內容寫回去lseek(fdConfig,0,SEEK_SET);write(fdConfig,readBuf,sizeOfConfig);//(6)關閉文件close(fdConfig);return 0; }運行結果:
文件運行參數: score=90 length=9 high=10整數寫入文件
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h>int main() {int fd;int data = 100;int data2 = 0;fd = open("./file1",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; }運行結果:
打開文件file1,發現像是亂碼,不用擔心,這只是字符顯示形式,不影響程序的執行結果。
結構體數組寫到文件
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h>struct Test {int a;char c; };int main() {int fd;struct Test data[2] = {{100,'a'},{101,'b'}};struct Test data2[2];fd = open("./file1",O_RDWR);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",data2[0].a,data2[0].c);printf("read %d %c\n",data2[1].a,data2[1].c);close(fd);return 0; }運行結果:
總結
以上是生活随笔為你收集整理的linux文件编程(3)—— main函数传参、myCp(配置成环境变量)、修改配置文件、整数和结构体数组写到文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hybrid框架UI重构之路:五、前端那
- 下一篇: matlab函数建模例题,数学建模mat