C 讀寫檔案

2021-10-12 00:22:11 字數 1511 閱讀 9659

txt檔案格式化讀寫(設定寬度、列對齊等)

char filename = 「檔名.txt」

(自己新建乙個檔案時,自己手動在檔名後面加上.txt可能會出錯)

//寫入txt檔案

char filename=

"檔名.txt"

ofstream outfile;outfile.

open

(filename,ios::out)

; outfile<<

setiosflags

(ios::left)

<<

setw(15

)<<

"number"

<<

setw(15

)<<

"name"

<<

setw(15

)<<

"birthday"

<<

setw(15

)<<

"gender"

<<

setw(15

)<<

"tele_number"

<<

setw(15

)<<

"address"

int i=

1;i<=max;i++

) outfile.

close()

;//讀取txt檔案

ifstream infile

(filename)

; string a;

node* node =

newnode()

;while

(infile>>node-

>data.number>>node-

>data.name>>node-

>data.birthday

>>node-

>data.gender>>node-

>data.telenumber>>node-

>data.address)

//判斷是否讀取完檔案的全部內容

infile.

close()

;

sprintf是乙個很實用的工具:

2.1 將多個數字轉化為字串,不夠指定位數的用0補齊

//將n轉化為固定長度的字串,不足位數的用0補齊 

string tostring

(int n)

如:

tostring

(111);

//00000111

2.2 將數字轉化為字串(不夠指定位數的用0補齊)並新增到另乙個字串(例如"name")後或者前。

char temp[

255]

;sprintf

(temp,

"name%08d\0"

, i)

;//i是數,如111、5

c 檔案讀寫 文字讀寫

include int main else return 0 格式 intfscanf file stream,constchar format,返回值 如果成功,該函式返回成功匹配和賦值的個數。如果到達檔案末尾或發生讀錯誤,則返回 eof 引數1 file stream 檔案指標 引數2 cons...

C 檔案讀寫

原文 http www.vckbase.com document viewdoc id 1439 原作 john peregrine file i o using c 序論 我曾發表過檔案輸入輸出的文章,現在覺得有必要再寫一點。檔案 i o 在c 中比烤蛋糕簡單多了。在這篇文章裡,我會詳細解釋asc...

C 讀寫檔案

1 使用filestream讀寫檔案 檔案頭 using system using system.collections.generic using system.text using system.io 讀檔案核心 byte bydata new byte 100 char chardata ne...