C++——文件IO

编写任务:编写一个文件复制程序,功能将一个文件复制到另一个文件。
ifstream in;//声明一个文件输入流对象
in.open("text.txt");//以输入方式打开一个文件test.txt

ofstream out;//声明一个文件输出流对象
out.open("text.txt");//以输出方式打开一个文件test.txt
in.close();//关闭文件
out.close();//关闭文件
c++语言实现:运用文件读写类ifream,oftream.以及输入流">>"输出流"<<";

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

int main(int argc,char *argv[])
{
        if(argc <3)
        {
        cout<<"please input 3 argument"<<endl;
        return -1;
        }

        ifstream in;
        in.open(argv[1]);//打开原文件
        if(!in)
        {
        cout<<"open in file failure"<<endl;
        return -2;
        }
        char buf[128]={0};
        
        ofstream out;
        out.open(argv[2]);//创建并打开新文件
        if(!out)
        {
        cout<<"open out file faillure"<<endl;
        return -3;
        }
        while(in>>buf)//in为空时跳出循环
        { 
        out<<buf;
        }

        in.close();//关闭文件流类对象
        out.close();
        
        return 0;
}   


以带模式的方式打开文件
ifstream in("test.txt",ios::app);//追加方式打开
ofstream out("test.txt",ios::app);
in.seekg(ios::beg);//ios::beg指向文件头,ios::end指向文件尾

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值