C 學習3 檔案操作入門

2021-09-12 20:46:58 字數 4065 閱讀 2949

常用的檔案函式及定義方式如下:

file* file = null;

file = fopen("test.txt", "r")

file *open;

int error;

error = fopen_s(&open, "test.yuv", "rb")

file *fopen(const char * path, const char * mode);

errno_tfopen_s( file** pfile, const char filename, const char mode );

intfscanf(filestream,const charformat,[argument…]);

intfprintf(file* stream, const char*format, [argument]);

size_tfread( void buffer, size_t size, size_t count, file stream) ;

size_tfwrite(const void buffer, size_t size, size_t count, file stream);

intfclose( file *fp );

file* writepu =

fopen

("output.txt"

,"a");

fprintf

(writepu,

"(%d,%d)\n"

, pu.blocks[channeltype]

.x, pu.blocks[channeltype]

.y);

fclose

(writepu)

;writepu =

null

;

標頭檔案如下:

/* read.h */

#include

#include

void

read_three_numbers()

;void

read_and_write_yuv_file()

;

/* write.h */

#include

#include

void

write_nine_numbers()

;void

write_yuv_file

(unsigned

char

*imageout_y,

unsigned

char

*imageout_cb,

unsigned

char

*imageout_cr)

;

/* read_three_numbers函式*/

void

read_three_numbers()

;for

(int i =

0; i <

3; i++

)fscanf

(read,

"%d"

,&a[i]);

//fscanf:讀取檔案的內容

for(

int i =

0; i <

3; i++

)printf

("%d\n"

, a[i]);

fclose

(read)

;//fclose:關掉檔案

read =

null

;//清理指標

}

/* write_nine_number函式*/

void

write_nine_numbers()

這裡增加了對yuv的處理方式,即對二進位制檔案的處理:

/* read.cpp 完整版*/

#include

#include

#include

"read.h"

#include

"write.h"

void

read_three_numbers()

;for

(int i =

0; i <

3; i++

)fscanf

(read,

"%d"

,&a[i]);

//fscanf:讀取檔案的內容

for(

int i =

0; i <

3; i++

)printf

("%d\n"

, a[i]);

fclose

(read)

;//fclose:關掉檔案

read =

null

;//清理指標

}void

read_and_write_yuv_file()

unsigned

char

*imagein_y,

*imagein_cb,

*imagein_cr;

//指向一片記憶體空間的指標

imagein_y =

(unsigned

char*)

malloc

(832

*480);

//開闢空間

imagein_cb =

(unsigned

char*)

malloc

(832/2

*480/2

);imagein_cr =

(unsigned

char*)

malloc

(832/2

*480/2

);fread

(imagein_y,

832*

480,

1, open)

;fread

(imagein_cb,

832/2*

480/2,

1, open)

;fread

(imagein_cr,

832/2*

480/2,

1, open)

;write_yuv_file

(imagein_y, imagein_cb, imagein_cr)

;//free(imagein_y);

//free(imagein_cb);

//free(imagein_cr);

fclose

(open)

;}

/* write.cpp 完整版*/

#include

#include

#include

"write.h"

void

write_nine_numbers()

void

write_yuv_file

(unsigned

char

*imageout_y,

unsigned

char

*imageout_cb,

unsigned

char

*imageout_cr)

/* main.cpp 完整版*/

#include

"read.h"

#include

"write.h"

intmain()

else

if(choose ==

'b')

else

printf

("error!");

system

("pause");

return0;

}

3 檔案操作

可以用來建立檔案。php 4,php 5,php 7,php 8 touch 設定檔案的訪問和修改時間 touch string filename,int time time int atime bool 嘗試將由filename給出的檔案的訪問和修改時間設定為給出的time。注意訪問時間總是會被修...

python 入門學習一一(四)檔案操作

python對檔案操作主要是通過匯入os import os 然後可以通過os來呼叫內建函式 1 os提供的一些內建函式及用法 1 2 os.getcwd 獲取當前工作目錄,即當前python指令碼工作的目錄路徑 3 os.chdir dirname 改變當前指令碼工作目錄 相當於shell下cd ...

實驗3 檔案操作

一 實驗目的與要求 學會使用檔案操作函式實現對檔案開啟 關閉 讀 寫等操作。學會對資料檔案進行簡單的操作。深入理解 c 的輸入輸出的含義及其實現方法。掌握標準輸入輸出流的應用。二 實驗過程 2 用隨機函式根據文字檔案的記錄數量生成乙個隨機數 3 根據這個隨機數,從所讀取的記錄中找到對應的記錄,並輸出...