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

時間 2021-07-08 22:33:34

1樓:泡影果果

從檔案中讀取資料,一行一行的讀取,用getline#include

fstream in;

cstring strfilename = "e:\\測試\\a.txt"; //路徑是雙斜槓

in.open(strfilename,ios::in); //ios_base::in

while( !in.eof()) //如果沒有讀到檔案結尾in.close();

2樓:水上漂湯

c還是c++?c++比c強大太多,學c++就要少用字元陣列c++版:

#include

#include

#include

using namespace std;

string readspecialword(string line, int num)

while (num--);

return str;

}int main()

3樓:阿四哥

c語言字串的學習,輸入指定字串,並且計算字串的位數

c++如何從檔案中讀取字串

4樓:匿名使用者

一般來說在c++語言中讀取txt檔案的資訊有三種方法:

1、使用c語言標準檔案i/o中的fopen()、fread()等等函式。示例如下(vc6.0下編譯通過):

#include

file*stream;

void main(void)

}2、使用c++語言中的檔案i/o中的ofstream,ifstream,fstream。示例如下(vc6.0下編譯通過):

#include

#include

#include

using namespace std;

int main ()

while (!in.eof() )

return 0;

}3、使用作業系統中的api函式,比如windows上的readfile()、openfile()等等,現在作業系統一般都具備記憶體檔案對映功能,對於大的txt檔案,一般都使用這種方式操作。

C語言如何輸入一行有空格的字串?

c語言輸入一行有空格的字串的方法 1 對於char char s 100 1000 第二個參數列示允許輸入的最大長度while 1000 輸入輸出樣例。輸入 he llo 輸出 he llo 2 對於string string s getline cin,s while getline cin,s ...

c中如何計算字串中某個指定字元的個數

幸運的雨祭 使用函式indexof 來檢索字串中所需要的字元出現的次數。如下 class program static void main string args 統計出字串中,下雪出現的次數 string text 今天下雪了嗎,明天不會下雪了吧,什麼時候才不下雪啊,我要去上學啊!string k...

C和C 如何將讀取有空格的字串

第一種 對於char型的 用 cin.getline char a 100 cin.getline a,100,終止符 當你把終止符設為回車的時候 就可以讀取空格了第二種 對於string型的 string a getline cin,a,終止符 c 推薦使用string 因為有很多庫函式至於按空格...