c 基礎入門 8

2021-10-09 13:57:06 字數 1228 閱讀 2600

檔案操作:

ofstream ofs;

ofs.

open

("test.txt"

,ios::out)

; ofs<<

"姓名:張三"

close()

; ifstream ifs;

ifs.

open

("test.txt"

,ios::in);if

(!ifs.

is_open()

) 第一種讀取方式:

char buf[

1024]=

;//初始化全為0

while

(ifs >> buf)

ifs.

close()

; 第二種讀取方式:

char buf[

1024]=

;//初始化全為0

while

(ifs.

getline

(buf,

sizeof

(buf)))

ifs.

close()

; 第三種讀取方式:

string buf;

while

(getline

(ifs,buf)

)char c;

while

((c=ifs.

get())

!=eof

)二進位制寫檔案:

ofs.

open

("person.txt"

,ios::out | ios::binary)

; person p =

; ofs.

write((

const

char*)

&p,sizeof

(p))

; ofs.

close()

強轉為const

char

*二進位制讀檔案:

ifs.

open

("person.txt"

,ios::in | ios::binary)

; person p;

ifs.

read((

char*)

&p,sizeof

(person));

ifs.

close()

;

C 基礎入門 8 結構體

結構體屬於使用者自定義的資料型別,允許使用者儲存不同的資料型別 語法 struct 結構體名 通過結構體建立變數的方式有三種 示例 結構體定義 struct student stu3 結構體變數建立方式3 int main cout 姓名 stu2.name 年齡 stu2.age 分數 stu2....

Python基礎入門 8

重點 ft.open ft.read ft.write ft.readline ft.readlines ft.close 作業 1.整理檔案操作中open函式裡邊第二個引數.表 2.將上邊每個函式的用法自己寫例子整理並理解 作用域 針對於變數 在函式中的使用情況 區域性作用域 函式作用域 閉包以外...

C 入門詳解 8

字段 field 是一種表示與物件或型別 結構體關聯的變數 欄位是型別的成員,舊稱 成員變數 與物件關聯的字段也叫做例項字段 與型別關聯的字段稱為靜態字段,由static修飾 參見c 語言定義文件 儘管字段宣告帶有分號,但它不是語句 欄位的名字一定是名詞 無顯示初始化時,字段獲得其型別的預設值,所以...