把乙個檔案到追加另外乙個檔案上

2021-04-06 17:02:34 字數 1797 閱讀 5454

下面這個例子演示了如何開啟和關閉檔案,如何讀取和儲存檔案,如何鎖定和解鎖檔案。這個程式的功能是把乙個檔案上的資料追加到另外乙個檔案結尾位置。這個程式開啟檔案並且把檔案中的資料追加到只允許當前程式執行儲存的檔案中,但是允許其它程序開啟並且讀取正在被當前程序追加的檔案。為了使讀者對檔案有乙個深入的理解,我們在寫的過程中呼叫了檔案鎖定功能。

這個程式使用

createfile

開啟兩個檔案。開啟

one.txt

是為了讀取其中的資料,開啟

two.txt

檔案是為了執行追加,同時

two.txt

檔案還支援共享讀功能,也就是允許其它程序讀取這個檔案。程式先讀取第乙個檔案中內容再把讀取到的內容追加到第二個檔案末尾,呼叫

readfile

和writefile

函式實現讀取和儲存

4k的內容。在寫第二個檔案之前,程式先呼叫

setfilepointer

函式移到檔案末尾,同時使用

lockfile

鎖定要執行寫操作的區域,執行鎖定功能是為了防止在寫的過程中其它擁有當前控制代碼副本的執行緒或程序訪問這塊區域。執行完一次寫的功能,我們就會呼叫

unlockfile

對這塊區域進行解鎖。在執行完鎖定功能之後我們就沒有必須再呼叫

setfilepointer

函式,因為只可能有乙個程序能夠執行寫的操作。

handle hfile;

dworddwbytesread, dwbyteswritten, dwpos;

bytebuff[4096];

// open the existing file.

hfile = createfile(text("one.txt"), // open one.txt

generic_read,// open for reading

0,// do not share

null,// no security

open_existing,// existing file only

file_attribute_normal,// normal file

null);// no attr. template

if (hfile == invalid_handle_value)

// open the existing file, or if the file does not exist,

// create a new file.

generic_write,// open for writing

file_share_read,// allow multiple readers

null,// no security

open_always,// open or create

file_attribute_normal,

// normal file

null);// no attr. template

// lock the second file to prevent another process from

// accessing it while writing to it. unlock the

// file when writing is finished.

do

} while (dwbytesread == sizeof(buff));

// close both files.

closehandle(hfile);

把乙個檔案到追加另外乙個檔案上

下面這個例子演示了如何開啟和關閉檔案,如何讀取和儲存檔案,如何鎖定和解鎖檔案。這個程式的功能是把乙個檔案上的資料追加到另外乙個檔案結尾位置。這個程式開啟檔案並且把檔案中的資料追加到只允許當前程式執行儲存的檔案中,但是允許其它程序開啟並且讀取正在被當前程序追加的檔案。為了使讀者對檔案有乙個深入的理解,...

把乙個檔案中的內容,複製到另外乙個檔案中

把乙個檔案中的內容,複製到另外乙個檔案中。基礎目錄 base dir e python重新學習 python核心程式設計 檔案讀寫 讀取的檔名 read file 演示檔案讀取的文字檔案.txt 寫進的檔名 write file 演示檔案的寫入.txt defmain 二進位制形式開啟唯讀檔案 fi...

把乙個表中的資料插入到另外乙個表中

1.在oracle中可以用下面兩種 01 create table newtable as select from oldtable 用於複製前未建立新錶newtable不存在的情況 02 insert into newtable select from oldtable 已經建立了新錶newtab...