C++文件输入输出处理(4)--文件复制

#include<iostream>
#include<fstream>
using namespace std;
int copy(char  *destFile,char*srcFile)
{
	char ch;ifstream inFile(srcFile,ios::in);
	ofstream outFile(destFile,ios::out);
	if(!srcFile)
	{
		cerr<<"File could not be open."<<endl;
		return 0;
	}
		if(!destFile)
	{
		cerr<<"File could not be open."<<endl;
		return 0;
	}
	while(inFile.get(ch))
   outFile.put(ch);
   inFile.close();outFile.close();
   cout<<"Finish."<<endl;
   return 1;
}
int main()
{    
	copy("F:\\copystudents.txt","F:\\students.txt");
}
	
	
	 

#include<iostream>
#include<fstream>
using namespace  std;
struct student
{
	int number;
	char name[30];int score;
};
const student mask={ 0,"noName\0",0};
int main()
{ 
   char s[80];
   student stu;
   ifstream	 instuf("d:\\students.txt",ios::in);
	ofstream outf("d:\\students.dat",ios::out);
	if(!instuf|!outf)
	{
		cerr<<"file couldnt't be open."<<endl;abort();
	}
	instuf.getline(s,80);
	while(instuf>>stu.number>>stu.name>>stu.score)
	{
		cout<<stu.number<<' '<<stu.name<<' ' <<stu.score<<endl;
		outf.write((char*)&stu,sizeof(student));
	}
	outf.write((char*)&mask,sizeof(student));
	instuf.close();
	outf.close();
}
发布了122 篇原创文章 · 获赞 14 · 访问量 6165

猜你喜欢

转载自blog.csdn.net/weixin_44001521/article/details/103972999
今日推荐