wendy890711

檔案讀取寫入判斷個數

Apr 30th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10. char str[80];
  11. int count=0;
  12. fstream file1;
  13. file1.open("C:\\text.txt",ios_base::in );
  14. fstream file2;
  15. file2.open("C:\\text2.txt",ios_base::out);
  16. if (!file1 || !file2)
  17. cout << "檔案開啟失敗" << endl;
  18. else
  19. {
  20. file1.getline(str,80);
  21. cout << str << endl;
  22. file2 << str << endl;
  23. for (int i = 0; i < 80; i++)
  24. {
  25. if (str[i] == 3)
  26. count++;
  27. }
  28.  
  29. file1.getline(str,80);
  30. cout << str << endl;
  31. file2 << str << endl;
  32. for (int i = 0; i < 80; i++)
  33. {
  34. if (str[i] == 3)
  35. count++;
  36. }
  37. }
  38.  
  39.  
  40. cout << "3的個數有" << count << "個" << endl;
  41. }
Add Comment
Please, Sign In to add comment