C C 讀寫txt檔案

2021-10-05 20:00:43 字數 1310 閱讀 2425

讀寫txt檔案

#include

std::string filename = username +

".txt"

;//檔名

std::ofstream writefile

(filename)

; writefile << password <<

"\n"

;//寫入password內容

std::ifstream readfile

(filename)

;std::string p;

readfile >> p;

//讀取內容存入p

按行讀取txt檔案資料存入陣列:

std::string c;

int comp;

int compa[4]

;//該檔案有4行內容

int i =0;

std::string f1 =

"mean\\"

+ u1 +

".txt"

;//檔名

std::ifstream read1

(f1)

;//開啟檔案

while

(std::

getline

(read1, c)

)//每次讀一行的資料進入c中。

read1.

close()

;

寫入txt檔案:

//乙個字元用char;

//字串用char *,表示char陣列;

//const表示傳進來的不改變它的內容。

void writetxt(const

char

* user)

讀取txt檔案:

c語言讀取每一行文字

char c[16]

;int comp;

int compa[4]

;char f1[64]

="";//該字串儲存檔名

sprintf

(f1,

"mean\\%s.txt"

, u1)

;//檔名

file *fp1;

fp1 =

fopen

(f1,

"r")

;for

(int i =

0; i <

4; i++

)fclose

(fp1)

;}

建立資料夾

讀寫TXT檔案

1 file.writealltext 寫入內容,可以指定編碼 寫入文字使用 file.writealltext 檔案路徑 例如 d 文字.txt 待寫入文字 public static void writetxtbyfiletext string filepath,string msg 2 fil...

VB讀寫TXT檔案

private subcommand1 click 讀檔案 open d a.txt for inputas 1dim lines asstring dimnextline asstring dimi asinteger dowhile noteof 1 onerror resume next li...

javascript 讀寫txt檔案

讀檔案 var fso,f1,ts var forreading 1 fso new activexobject scripting.filesystemobject ts fso.opentextfile document.all.attachfile.value 此處為含全部路徑的檔名 forr...