溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++中怎么實現文件流操作

發布時間:2021-07-14 17:03:28 來源:億速云 閱讀:254 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關C++中怎么實現文件流操作,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

C++文件流操作之文件寫入:

#include < fstream> #include < iostream>   using namespace std;  int main(){  string str;  ofstream out("d.txt");  str="床前明月光\n疑是地上霜\n舉頭望明月\n低頭思故鄉\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++文件流操作之文件復制

#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< < "文件復制成功!!!";  return 0;   }

篩法判斷素數程序

#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文件中寫入一系列小于10000的整數,并換行,就可以實現素數判斷,因為也涉及文件的讀取操作,所以一并給出。

以上就是C++中怎么實現文件流操作,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

c++
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女