日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

linux文件编程(3)—— main函数传参、myCp(配置成环境变量)、修改配置文件、整数和结构体数组写到文件

發(fā)布時(shí)間:2023/12/10 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux文件编程(3)—— main函数传参、myCp(配置成环境变量)、修改配置文件、整数和结构体数组写到文件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考:linux文件編程(3)—— 文件編程的簡(jiǎn)單應(yīng)用:myCp、修改配置文件
作者:丶PURSUING
發(fā)布時(shí)間: 2021-04-09 23:45:05
網(wǎng)址:https://blog.csdn.net/weixin_44742824/article/details/115209404
部分參考:https://leo-max.blog.csdn.net/article/details/112966334

目錄

  • 實(shí)現(xiàn)cp指令
    • 掌握向main函數(shù)傳參
    • cp指令實(shí)現(xiàn)思路
    • 實(shí)現(xiàn)代碼
    • myCp指令全局可用
  • 修改配置文件
  • 整數(shù)寫(xiě)入文件
  • 結(jié)構(gòu)體數(shù)組寫(xiě)到文件

實(shí)現(xiàn)cp指令

掌握向main函數(shù)傳參

int main(int argc,char** argv )/** 參數(shù) argc: 參數(shù)個(gè)數(shù) **argv:二級(jí)指針,數(shù)組的指針(argv[0]、argv[1]、argv[2]...),即這個(gè)指針里的每一項(xiàng)都是一個(gè)數(shù)組(字符串) **/

最直觀的還是例子

#include <stdio.h>int main(int argc,char** argv) {printf("參數(shù)個(gè)數(shù):%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; }

運(yùn)行結(jié)果:

cp指令實(shí)現(xiàn)思路

(1)打開(kāi)src源文件(要被復(fù)制的文件)
(2)把源文件的內(nèi)容讀入buf
(3)創(chuàng)建目標(biāo)文件
(4)把buf內(nèi)容寫(xiě)入目標(biāo)文件
(5)關(guān)閉源文件與目標(biāo)文件

實(shí)現(xiàn)代碼

(1)精簡(jiǎn)不帶調(diào)試版

#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)打開(kāi)src源文件(要被復(fù)制的文件)int fdSrc = open(argv[1],O_RDWR);int sizeOfSrc = lseek(fdSrc,0,SEEK_END);lseek(fdSrc,0,SEEK_SET);//(2)把源文件的內(nèi)容讀入bufchar* readBuf = (char* )malloc(sizeOfSrc);memset(readBuf,'\0',sizeOfSrc);int retRead = read(fdSrc,readBuf,sizeOfSrc);//(3)創(chuàng)建目標(biāo)文件,如果已經(jīng)存在則覆蓋 //O_TRUN原文件度先截為0int fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);//(4)把buf內(nèi)容寫(xiě)入目標(biāo)文件int retWrite = write(fdDes,readBuf,sizeOfSrc);//(5)關(guān)閉源文件與目標(biāo)文件close(fdSrc);close(fdDes);return 0; }

(2)打印調(diào)試信息對(duì)open,read,write的狀態(tài)進(jìn)行跟蹤版

#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("參數(shù)個(gè)數(shù)錯(cuò)誤\n");exit(-1);}//(1)打開(kāi)src源文件(要被復(fù)制的文件)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)把源文件的內(nèi)容讀入bufint retRead = read(fdSrc,readBuf,sizeOfSrc);if(retRead < 0){printf("read error\n");exit(-1);}//(3)創(chuàng)建目標(biāo)文件,如果已經(jīng)存在則覆蓋 //O_TRUN原文件長(zhǎng)度截為0int fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);if(fdDes < 0){printf("open Des error\n");exit(-1);}//(4)把buf內(nèi)容寫(xiě)入目標(biāo)文件int retWrite = write(fdDes,readBuf,sizeOfSrc);if(retWrite < 0){printf("write error\n");exit(-1);}//(5)關(guān)閉源文件與目標(biāo)文件close(fdSrc);close(fdDes);return 0; }

運(yùn)行結(jié)果:生成了和mainPro.c 一樣的new.c

myCp指令全局可用

(1)查看當(dāng)前所配置的環(huán)境變量

echo $PATH

發(fā)現(xiàn)并沒(méi)有myCp可執(zhí)行文件的路徑,所以要手動(dòng)添加進(jìn)去。

在此之前,還可以創(chuàng)建一個(gè)文件夾,專(zhuān)門(mén)用來(lái)存放我們自己寫(xiě)的命令,如myCp,myLs等等,方便全局使用,而不僅僅限于所在路徑下使用了。使用方法直接myCp XX XX(不必再使用./myCp XX XX這種方法了)。

(2)創(chuàng)建文件夾,把myCp放進(jìn)去

mkdir MYPATH

(3)pwd查看當(dāng)前路徑為:

/home/zhua/wenJian/MYPATH
  • 1

(4)添加路徑:

export PATH=$PATH:/home/zhua/wenJian/MYPATH

其中$PATH也可替換成上面圖片所顯示的環(huán)境變量,即:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:

(5)再次查看配置的環(huán)境變量:配置成功(在最后尾巴上,此時(shí)只要在這個(gè)文件夾下的可執(zhí)行命令都可以在全局運(yùn)行)

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/zhua/wenJian/MYPATH

修改配置文件

現(xiàn)有某配置文件如config,要求利用文件編程把length的值修改為9.

文件運(yùn)行參數(shù): score=90 length=5 high=10

簡(jiǎn)單示例:

#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)打開(kāi)配置文件int fdConfig = open("./config",O_RDWR);//(2)計(jì)算配置文件大小int sizeOfConfig = lseek(fdConfig,0,SEEK_END);lseek(fdConfig,0,SEEK_SET);char* readBuf = (char* )malloc(sizeOfConfig);memset(readBuf,'\0',sizeOfConfig);//(3)讀取文件內(nèi)容到readBufread(fdConfig,readBuf,sizeOfConfig);//(4)找到并修改length=后面的內(nèi)容char* p = strstr(readBuf,"length=");p = p + strlen("length=");*p = '9';//(5)重新將內(nèi)容寫(xiě)回去lseek(fdConfig,0,SEEK_SET);write(fdConfig,readBuf,sizeOfConfig);//(6)關(guān)閉文件close(fdConfig);return 0; }

運(yùn)行結(jié)果:

文件運(yùn)行參數(shù): score=90 length=9 high=10

整數(shù)寫(xiě)入文件

#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; }

運(yùn)行結(jié)果:

打開(kāi)文件file1,發(fā)現(xiàn)像是亂碼,不用擔(dān)心,這只是字符顯示形式,不影響程序的執(zhí)行結(jié)果。

結(jié)構(gòu)體數(shù)組寫(xiě)到文件

#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; }

運(yùn)行結(jié)果:

總結(jié)

以上是生活随笔為你收集整理的linux文件编程(3)—— main函数传参、myCp(配置成环境变量)、修改配置文件、整数和结构体数组写到文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。