新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C++文件流操作具體實(shí)現(xiàn)方法探討
C++編程語(yǔ)言在實(shí)際編程中,對(duì)于文件的操作是一個(gè)比較簡(jiǎn)單的操作,大家可以通過(guò)一些簡(jiǎn)單的實(shí)例就能充分的掌握這一應(yīng)用技巧,并在實(shí)際編程中給自己的程序開(kāi)發(fā)帶來(lái)一些幫助。下面就讓我們一起來(lái)看看C++文件流操作的相關(guān)應(yīng)用技巧吧。

C++文件流操作之文件寫(xiě)入:
- #include < fstream>
- #include < iostream>
- using namespace std;
- int main(){
- string str;
- ofstream out("d.txt");
- str="床前明月光\n疑是地上霜\n舉頭望明月\n低頭思故鄉(xiāng)\n";
- out< < str< < endl;
- return 0;
- }
C++文件流操作之文件讀?。?/strong>
- #include < fstream>
- #include < iostream>
- using namespace std;
- int main(){
- ifstream in("a.txt");
- for(string str;getline(in,str);)
- cout< < str< < "\n";
- return 0;
- }
C++文件流操作之文件復(fù)制
- #include < fstream>
- #include < iostream>
- using namespace std;
- int main(){
- ifstream in("a.txt");
- ofstream out("b.txt");
- for(string str;getline(in,str);)
- out< < str< < endl;
- cout< < "文件復(fù)制成功!!!";
- return 0;
- }
篩法判斷素?cái)?shù)程序
- #include < iostream>
- #include < vector>
- #include < fstream>
- using namespace std;
- int main(){
- vector< int> prime(10000,1);
- for(int i=2;i< 100;++i)
- if(prime[i])
- for(int j=i;i*j< 10000;++j)
- prime[i*j]=0;
- ifstream in("c.txt");
- for(int a;in>>a && a>1 && a< 10000;)
- cout< < a< < " is "< < (prime[a]?"":"not ")< < " a prime.\n";
- }
c.txt文件中寫(xiě)入一系列小于10000的整數(shù),并換行,就可以實(shí)現(xiàn)素?cái)?shù)判斷,因?yàn)橐采婕拔募淖x取操作,所以一并給出。
感覺(jué)C++文件流操作好esay啊!Java讀取文件還要復(fù)雜一點(diǎn)。
新聞標(biāo)題:C++文件流操作具體實(shí)現(xiàn)方法探討
文章網(wǎng)址:http://www.dlmjj.cn/article/coijchs.html


咨詢
建站咨詢
