腳本開發-檔案讀寫操作
by:授客 QQ:1033553122
函數說明
函數原型:
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *file_pointer );
參數說明:
buffer:包含資料的緩沖區
size:緩沖區buffer的大小(位元組為機關)
count:緩沖區中寫入多少個size大小資料(The number of bytes to store in the buffer)
file_pointer:指向存放資料檔案的檔案指針
size_t: 際寫入的資料塊數目(每個資料塊大小為size)
size_t fread( void *buffer, size_t size, size_t count, FILE *file_pointer );
同fwrite
函數原型:
int ferror( FILE *file_pointer );
功能:檢查檔案i/o錯誤
file_pointer:指向檔案的檔案指針
函數原型
int feof( FILE *file_pointer );
功能:檢測流上的檔案結束符
file_pointer:指向檔案的檔案指針
以下通過例子說明如何進行資料存取操作
例子1:
從d盤的readme.txt中讀取檔案内容并顯示讀取位元組數
代碼:
Action()
{
int count, total = 0;
char buffer[1000];
long file_stream;
char * filename = "d:\\readme.txt";
if ((file_stream = fopen(filename, "r")) == NULL )
{
lr_error_message ("Cannot open %s", filename);
return -1;
}
// Read until end of file
while (!feof(file_stream))
// Read 1000 bytes while maintaining a running count
count = fread(buffer, sizeof(char), 1000, file_stream);
lr_output_message ("= bytes read", count);
if (ferror(file_stream)) {
lr_output_message ("Error reading file %s", filename);
break;
}
total += count; // Add up actual bytes read
// Display final total
lr_output_message ("Total number of bytes read = %d", total );
// Close the file stream
if (fclose(file_stream))
{
lr_error_message ("Error closing file %s", filename);
return 0;
}
結果:
例子2:
将每個學生的資料(學号和成績)儲存到某個檔案,然後從該檔案讀取存放的學生資料記錄并展示
// 定義儲存學生資料的檔案名
#ifdef unix
char * filename = "/tmp/marks.txt";
#else
char * filename = "d:\\marks.txt";
#endif
// 儲存學生資料(學号和成績)
struct student_data
int student_id;
unsigned char marks[10];
};
struct student_data sd1; // 定義結構體,用于定義學生資料
struct student_data sd2; // 用于存放從檔案中讀取的學生資料
long file_handle = NULL; // 檔案句柄
int i = 0;
int count = 0; // 統計每次讀取學生記錄個數
int total = 0; // 用于統計總的學生記錄個數
//
打開、建立資料檔案
if ((file_handle = fopen(filename, "w+")) == NULL)
{
lr_output_message ("Unable to create %s", filename);
return -1;
}
設定學号
sd1.student_id = 1001;
設定成績(假設10門課程)
for (i = 0; i < 10; i++)
sd1.marks[i] = (unsigned char)(85 + i);
把學生記錄寫入資料檔案
i = fwrite(&sd1, sizeof(struct student_data), 1,
file_handle);
if (i > 0) //
fwrite調用不成功則傳回0
lr_output_message ("Successfully wrote %d record", i);
fclose(file_handle); //
關閉檔案
if ((file_handle = fopen(filename, "r")) == NULL ) //
如果打開檔案失敗
lr_error_message ("Cannot open %s", filename);
讀取全部檔案内容
while (!feof(file_handle))
count = fread(&sd2, sizeof(struct student_data), 1,
lr_output_message ("%d
個學生資料記錄",
count);
檢查檔案I/O錯誤
if (ferror(file_handle))
lr_output_message ("Error reading file %s", filename);
break;
total += count; //
計算實際讀取的總位元組數
展示最後的資料
lr_output_message("學生的學号為:%d,成績如下:",
sd2.student_id);
for(i=0; i<10; i++)
lr_output_message("%u", sd2.marks[i]);
lr_output_message ("Total number of student record = %d",
total);
關鍵檔案流
if (fclose(file_handle))
lr_error_message ("Error closing file %s", filename);
return 0;
運作結果:
作者:授客
QQ:1033553122
全國軟體測試QQ交流群:7156436
Git位址:https://gitee.com/ishouke
友情提示:限于時間倉促,文中可能存在錯誤,歡迎指正、評論!
作者五行缺錢,如果覺得文章對您有幫助,請掃描下邊的二維碼打賞作者,金額随意,您的支援将是我繼續創作的源動力,打賞後如有任何疑問,請聯系我!!!
微信打賞
支付寶打賞 全國軟體測試交流QQ群