文件編程練習

自己實現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.pswp.cn/news/272233.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/272233.shtml
英文地址,請注明出處:http://en.pswp.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