讀取文字檔案中指定的幾列

2022-02-13 07:51:35 字數 1567 閱讀 6566

今天在論壇上看到乙個問題,這個問題大概是這樣的,讀取文字檔案中指定的幾列,並儲存入資料庫中。

insus.net花上些少時間,實現讀取文字的功能部分。

建立乙個文字檔案,文字新增好幾行。每一行有好幾列,列與列之間使用逗號分隔。

adf,adfadf,adsfgads,qwer,wrt,wrey,asdfs,qwer,tryerqert6uy7,468u4,578,qewrqew,qwertwet,67u46,45676,45qer87,47857,et356y,etyedgh

adsadf,adfadf,adsfgads,qwer,wrt,wrey,asdfs,qwer,tryert6uy7,468u4,5zxc78,qewrqew,qwertrtwet,67u46,456qer76,4wu1587,47wr857,etyty,etyedgh

adwrf,adfadf,adsfgads,qwuier,wrqert,wrey,aswrtdfs,qwer,try790ert6uy7,468u4,572458,qewrqew,qwertwet,67u46,45676,4rwt587,47857,etuoipy,etyedgh

ad356f,adf1324adf,adsfgads,qwasdfer,wrt,wrey,asdfs,qwer,tryert6uy7,468u4,5rt78,qewrqew,qwertwetyet,67u46,45676,4zcxv587,47857,ety,etyedgh

adf356,adfadf,adsfga34ds,qwer,wrt,wrey,asdfs,qwer,tryert6uy7,468u4,578,qewwtrrqew,qwerqertwet,67u4ety6,45676,4587,47857,etwey,etyedgh

ad869f,adfeadf,adsfqrgads,qwer,wrt,wrey,as689dfs,qwer,trytyert6uy7,468u4,578,qewrqew,qweroyiotwet,67u46,45676,4535687,47857,ety,etyedgh

你如果也想使用這些文本來演示,可以拷貝並儲存為.txt檔案。

接下來可以實現了,不管文字檔案是否有列名,這不是關心的,關心的是資料表的列名即可。

以資料表寫作乙個model,如:

這個類別中,你可以看到乙個解釋函式:tryparase();它是處理文字資料行的。實現了讀取文字檔案每乙個前10列。當然讀取多少列,你可以另改啦。

還有,你還要注意文字每一行是否有空行,每行頭尾是否有空格。列數是否夠,是否有其它規則的分隔字元等。

然後你就可以在程式中使用這個數與函式了:

上圖的**中,insus.net是把獲取的資料顯示於gridview控制項中,省略了儲存入資料庫的功能。

最後是顯示結果:

讀取文字檔案

void ctestdlg onreadinfo cfile filewrite1 testwrite1.txt cfile modecreate cfile modewrite cfile filewrite2 testwrite2.txt cfile modecreate cfile modew...

讀取文字檔案內容

讀取文字檔案內容 param filepathandname 帶有完整絕對路徑的檔名 param encoding 文字檔案開啟的編碼方式 return 返回文字檔案的內容 public string readtxt string filepathandname,string encoding th...

linux c getline讀取文字檔案

函式原型 istream getline istream is,string str,char delim istream getline istream is,string str 讀取檔案,第乙個按引數delim作為分隔符,第二個以 n 作為分隔符。問題產生 在windows,新建文字文件,輸入...