linux下c語言程式拷貝檔案

2021-09-21 20:06:54 字數 2263 閱讀 4867

localhost login:root

password:******

//登陸字元介面

[root@localhost root]#mkdir dir1 dir2

//在root目錄下,分別建立dir1,dir2兩個資料夾

[root@localhost root]#ll

//檢視剛建立的dir1,dir2資料夾

[root@localhost root]#cd dir1

//切換到dir1資料夾

[root@localhost dir1]#touch filea

//在dir1建立乙個名為filea檔案

[root@localhost dir1]#cd ..

//返回上級目錄

[root@localhost root]#cd dir2

//切換到dir2資料夾

[root@localhost dir2]#touch fileb

//在dir2建立乙個名為fileb檔案

[root@localhost dir2]#ls –l

//顯示fileb檔案

[root@localhost root]#cd

//切換到root目錄

[root@localhost root]#vi helloworld.c

//建立乙個helloworld.c原始檔

#include

#include //輸入輸入函式標頭檔案

int mian(void)       //主函式

if((fb=fopen(「/root/dir2/fileb」,」w+」))==null)

while((ch=getchar())!=eof)   //while判斷語句,拷貝字串,當遇到eof結束

fputc(ch,fa); //將ch獲取的字串,拷貝到fa指標中

rewind(fa); //第乙個while迴圈結束時fp所指檔案的讀寫位置在檔案末尾,然後呼叫rewind函式把讀寫位置移到檔案開頭,再進入第二個while迴圈從頭讀取檔案內容。

while((ch=fgetc(fa))!=eof) //讀取字串,當遇到eof時,取fa指標的字串,給ch

fputc(ch,fb); //將ch獲取的字串,拷貝到fb指標中

fclose(fa); //關閉指標fa

fclose(fb); //關閉指標fb

return 0;  //執行完成後返回0 }

[root@localhost root]#gcc –o mycp helloworld.c

//使用gcc處理helloworld.c原始檔,一步完成編譯、彙編和連線。使用了-o,同時輸出指定的檔案可執行檔案mycp,如果不指定,將使用預設的檔名a.out

[root@localhost root]#./mycp

//執行檔案mycp

hello world!

//輸入hello world!

並回車,這時第一次呼叫

getchar

返回,讀取字元

h存到檔案中,然後連續呼叫

getchar12

次,讀取

hello world!

和換行符存到檔案中,第

14次呼叫

getchar

又阻塞了

,(如果在輸入字元並回車,就開始第

14次呼叫

getchar

函式,一樣是遇到換行符後呼叫

getchar

又會阻塞。)最後輸入

ctrl+d

,第14

次呼叫getchar

,返回的是

eof, 

跳出迴圈,進入下乙個迴圈,回到檔案開頭,把檔案內容乙個位元組乙個位元組讀出來列印,直到檔案結束

[root@localhost root]#ll

//顯示可見檔案顯示資訊

[root@localhost root]#vi ./dir1/filea

//開啟/root/dir1/filea

目錄下檔案,「

.」指的是當前目錄

//顯示./dir/filea

目錄檔案,

hello world!

按esc

鍵:q!

不儲存強行退出

//開啟/root/dir2/fileb

目錄檔案,顯示

./dir2/fileb

內容hello world!

Linux下C語言實現檔案拷貝 轉

linux下c語言實現檔案拷貝 function copy file from file1 to file2 how to execute copyfile file1 file2 under linux data 2007 05 09 include fprintf stderr,bufsiz i...

linux下c語言 讀取檔案

linux讀取檔案要用到stdio.h檔案,在 usr include下包含大部分的c標頭檔案,sys types.h也位於其中,usr src linu 版本號 存放有你的核心源 在linux下讀檔案也是fopen 檔名 方式 方式有r,w等,下面為一段讀檔案的 cat 7 3.c 1 inclu...

Linux下U盤拷貝檔案

建立乙個資料夾 mkdir mnt usb 掛載u盤 掛u盤之前,執行命令cat proc partitions,看看現在系統中有哪些分割槽。插上u盤以後,再次執行上述命令,看看多出來什麼分割槽 通常是sda1,ps由於偶是在虛擬機器下裝的,所以是sdb1 1 插入u盤 2 輸入 fdisk l d...