Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- void main()
- {
- fstream file1;
- string infile1;
- file1.open("c:\\test.txt", ios_base::in);
- if (!file1)
- cout << "檔案開啟失敗\n";
- else
- {
- int counter=0;
- while (!file1.eof()) //若到達檔案結尾則”file>>str”將 傳回0, 跳出while回圈
- {
- char str;
- file1.get(str);
- if (!file1.good()) //如果讀取的字元是不正常的則跳出迴圈
- break;
- if (str == '3')
- counter++;
- infile1 += str;
- }
- cout <<"讀取到:\n"<< infile1 << endl << endl;
- fstream file2;
- file2.open("c:\\test2.txt", ios_base::out);
- if (file2)
- {
- file2 << infile1;
- }
- cout << "複製完畢\n共有 "<<counter<<" 個3\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement