c如何讀取檔案中的一部分字串內容

2021-03-08 13:08:03 字數 1724 閱讀 3851

1樓:匿名使用者

首先算出檔案內容長度,用file_l = sizeof(檔名);

其次把要比較的內容放到陣列buff1,檔案的內容放到陣列buff2,for(j=0;j<11;j++)//要比較11個字元for(i=0;i出本次盾環

}有了這些參考只要你懂一些除錯方法,相信你是能做出來的,如果我把全部程式貼出來,對你個人而言是沒好處的,此法請借籤,謝謝

2樓:tony羅騰

先將檔案全部讀入 char* 變數。再用 string 類 構建函式建一個string 物件,在把 char* 內容放入。

下面是將檔案全部讀入char * buffer;

/* fread example: read an entire file */

#include

#include

int main ()

// obtain file size:

fseek (pfile , 0 , seek_end);

lsize = ftell (pfile);

rewind (pfile);

// allocate memory to contain the whole file:

buffer = (char*) malloc (sizeof(char)*lsize);

if (buffer == null)

// copy the file into the buffer:

result = fread (buffer,1,lsize,pfile);

if (result != lsize)

/* the whole file is now loaded in the memory buffer. */

// terminate

fclose (pfile);

free (buffer);

return 0;

}構建函式建一個string 物件,把 char * buffer 內容存入 程式部分,請自己補充:

#include

#include

#include

using namespace std;

#include

// 插入上面程式 .....

// 補充

string sss;

sss.assign(buffer,result);

cout << sss << endl;

3樓:電腦崽

可以使用到字串函式的substr()函式,包含在標頭檔案中;

針對你上面的問題,可以先將檔案內容全部儲存在一個字串str中,(可以包括空格一起寫入,也可以不儲存空格;為了節約我這裡講的是不儲存空格的方式)

1、將檔案內容讀取出來並儲存到str中,那麼str中的內容是「mm5***010b1vss!vss!nmos_**3w=3.02426e-6l=350.498e-9」;

2、定義二個新的字串str1,str2;

3、給str1、str2賦值:

str1=str.substr(0,3);

str2=str.substr(19,8);

4、經過上面的處理後,str1、str2得到的字串就是你想要的了。

lz,關於str的相關函式還請你自己下去研究研究就能理解了!

4樓:匿名使用者

檔案字元讀出來放到陣列中 遇到空格或者換行符時進行串匹配

SQL語句,如何將欄位中的一部分字串作為條件查詢

select from 表 where substring code,2,1 0 and substring code,3,1 0 and substring code,4,1 0 and substring code,5,1 0 orselect from 表 where substring co...

運用C 如何讀取一行字串的指定字元

從檔案中讀取資料,一行一行的讀取,用getline include fstream in cstring strfilename e 測試 a.txt 路徑是雙斜槓 in.open strfilename,ios in ios base in while in.eof 如果沒有讀到檔案結尾in.cl...

c語言如何向檔案中輸入字串,c語言如何向一個檔案中輸入字串

1.通過fopen開啟檔案,fputs像檔案寫入資料,fclose關閉檔案。include int main 2.file fopen const char fname,const char mode fopen 函式開啟由fname 檔名 指定的檔案,並返回一個關聯該檔案的流.如果發生錯誤,fop...