Linux C 檔案建立與拷貝

2021-10-03 19:06:01 字數 1414 閱讀 2139

要求

下面所有功能要求使用c**實現 建立乙個文字檔案叫test.txt,裡面寫一段字串,至少有10行,其中某一行包括字串 「hello」;

從這個檔案裡面讀出每一行進行解析,並列印包含"hello"字串的這一行;

再把這個文字檔案拷貝乙份,重新命名為test.log

把test.log裡所有的內容列印到標準輸出;

fp1 =

fopen

("./test.txt"

,"w+");

if(fp1 ==

null

)for

(i=0

;i<

10;i++

)

從這個檔案裡面讀出每一行進行解析,並列印包含"hello"字串的這一行;

考慮:fgets, strstr, printf

while(!

feof

(fp1)

)

while(!

feof

(fp1)

)

把test.log裡所有的內容列印到標準輸出;

考慮: fgets(); printf();

fp3 =

fopen

("test.log"

,"r+");

if(fp3 ==

null

)while(!

feof

(fp3)

)fclose

(fp3)

;

執行結果:

[klaus@localhost chapt2]$ ls

buf buf_rewrite.c file file.c hello.txt write.c ww

[klaus@localhost chapt2]$ .

/file

buf:hello world!

buf:sdasfdhellowzdc!

mensljusjpks

mensljusjpks

mensljusjpks

mensljusjpks

mensljusjpks

hello world!

sdasfdhellowzdc!

mensljusjpks

mensljusjpks

mensljusjpks

mensljusjpks

mensljusjpks

[klaus@localhost chapt2]$ ls

buf buf_rewrite.c file file.c hello.txt test.log test.txt write.c ww

資料夾拷貝(linux c 遞迴)

int cpdir char fromdir,char todir mkdir todir,0755 while ptr readdir dir null ret strcmp ptr d name,if 0 ret snprintf fbuf,256,s s fromdir,ptr d name ...

LinuxC實現資料夾及檔案拷貝

linuxc實現資料夾及檔案拷貝 include include include include include include include 判斷是否為目錄 int is dir char path 處理字串的函式 int endwith char s,char c char str conta...

Linux C 實現mycp 可以顯示檔案拷貝進度

在linux系統裡面用到 cp命令複製不能顯示檔案拷貝的進度,也不能計算還有多長時間檔案可以 拷貝結束,現在寫乙個程式可以顯示檔案拷貝的進度。思路 當前目錄下面有乙個1g大小的bigfile檔案 當我在命令列下面輸入ls lh bigfile,我會得到這個檔案的詳細資訊,當然也可以看到檔案的大小。n...