Linux下檔案讀寫示例

2021-07-04 20:44:10 字數 880 閱讀 6729

1. 對於fread或fwrite檢視man手冊,其中要注意返回值。

2. 上述兩個函式第二個引數如果是1,那麼返回值即實際傳輸長度,如果返回值是0,一般表示檔案傳完;如果返回值等於需要傳輸長度,那麼正常;如果返回值小於指定傳輸長度,那麼有可能是最後一次傳輸,因為到達檔案尾,也有可能是傳輸錯誤;

如果第二個引數不是1,那麼就不能通過返回值判斷是否傳輸完,要通過對應庫函式來判斷是否到達檔案尾。如果返回小於傳輸長度,有可能是正常、有可能發生錯誤,也要通過庫函式檢測。

3. 示例原始碼如下:

#include

#include

#include

/*function : read data from the 2page.pdf,and write the date to write.pdf

*/char * readfilename = "./2page.pdf";

char * writefilename = "./write.pdf";

#define buffer_size 2048

void main()

file *writefp = fopen(writefilename, "w");

if(null == writefp) //if failed,fread and fwrite can't go on  /*

fread or fwrite does not distinguish between end-of-file and error, and callers must use feof(3)

and ferror(3) to determine which occurred.

*/readlengh  = 1024;

while(1)

else}}

QT下檔案的讀寫

1 不能在沒有對應目錄的情況下新建檔案,qfile只會新建檔案,qdir才會新建資料夾 例 在沒有新建目錄newdir的情況下,直接新建檔案newfile qfile file newdir newfile 結果失敗 2 如果對應目錄存在,newfile不存在,使用以下2行 檔案newfile依然不...

用Java讀寫檔案 示例

1.讀檔案 bufferedreader in new bufferedreader new inputstreamreader new fileinputstream d test.xml string xml in.readline 2.寫檔案 string str abc bufferedwr...

Linux 下檔案操作

include include include include include include include include include define s 100 void treedisplay void treecreat void filecreatopen void fileread ...