要求:
将某文件拷贝到另一个地址,并用sleep()函数控制进度。
#include<iostream>
#include<fstream>
#include<string>
#include<Windows.h>
using namespace std;
int main()
{
// 打开源文件fpO.txt和目标文件fpT.txt
// 注意源文件要和main.cpp在同一级哦
ifstream fin("fpO.txt");
ofstream fout("D:\\fpT.txt");
// 如果打开文件失败报告错误
if (!fin.is_open() || !fout.is_open())
{
cout << "Failed to open file!" << endl;
return -1;
}
cout << "Open file successfully!" << endl;
// buf取字符串
string buf;
// 设置两个计数器
int i = 0, j = 0;
// 循环读取、写入
while (!fin.eof())
{
fin >> buf;
fout << buf << " ";
i++; /* 记录读取次数 */
if (i%30==0) /*每取30个字符串休眠1秒*/
{
j++; /*记录休眠次数*/
Sleep(1000);
}
//清空字符
buf.clear();
}
cout << "fpO.txt has " << i << " strings." << endl;
cout << "Sleep for " << j << " seconds in total." << endl;
cout << "\nExecution completed, Please check fpT.txt!" << endl;
//关闭文件
fin.close();
fout.close();
system("pause");
return 0;
}
可能是程序太小了,sleep()函数看不出什么效果。
每天都要进步一点点哦~