//C语言
//编写一个文件复制程序
//功能将一个文件复制到另一个文件
//argc argv[]
//整型变量:argc 程序的参数数量,包含本身
//字符指针数组:argv[] 每个指针指向命令行的一个字符串
//in out 两个文件指针 类型:FILE *
//分别作为两个I/O流对象使用
#include
#include
int main(int argc,char* argv[])
{
FILE *in,*out;
int ch;
if(argc!=3)
{
fprintf(stderr,"输入形式:copyFile 原文件名 目标文件名 n");
exit(EXIT_FAILURE);
}
if((in=fopen(argv[1],"rb"))==NULL)
{
fprintf(stderr,"打不开传文件:%s n",argv[1]);
exit(EXIT_FAILURE);
}
if((out=fopen(argv[2],"wb"))==NULL)
{
fprintf(stderr,"打不开传文件:%s n",argv[2]);
fclose(in); //擦屁股
exit(EXIT_FAILURE);
}
while((ch=getc(in))!=EOF) //EOF== end of file
{
if(putc(ch,out)==EOF)
{
break;
}
}
if(ferror(in))
{
printf("读取文件 %s 失败!n",argv[1]);
}
if(ferror(out))
{
printf("写入文件 %s 失败!n",argv[2]);
}
printf("成功复制1个文件!n");
fclose(in);
fclose(out);
return 0;
}
//C++
//打开文件读取数据
#include
#include
using namespace std;
int main()
{
ifstream in;
in.open("test.txt");
if(!in)
{
cerr<<"打开文件失败"<>x)
{
cout<