从C看C++之(八)文件流操作

    Linux平台下提供了标准的C库API实现对文件的读写操作,同样C++也提供了自身对文件流操作的一些手段.虽然不知道以后会不会用到,但是了解一下是很有必要的.

源码:

#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{       char filename1[256],filename2[256];
        cout<<"Input source file name:  ";
        cin>>filename1;
        cout<<"Input destination file name:  ";
        cin>>filename2;
        ifstream infile(filename1);
        ofstream outfile(filename2);
        char ch;
        while(infile.get(ch))
                outfile.put(ch);
        infile.close();
        outfile.close();

        return 0;
}
    C++中要实现对文件流的操作,需要包含头文件"include <fstream>".本程序需要把一个文件里面的内容复制到另外一个文件,比如我们有一个已经存在的文件叫source.txt,里面有如下内容:

Hello,SE7EN:
	Remember,hope is a good thing!
    编译运行:

root@se7en-LIFEBOOK-LH531:~/learn/Cpp_Program# g++ file.cpp -o file
root@se7en-LIFEBOOK-LH531:~/learn/Cpp_Program# ./file 
Input source file name:  source.txt
Input destination file name:  dest.txt
root@se7en-LIFEBOOK-LH531:~/learn/Cpp_Program# cat dest.txt 
Hello,SE7EN:
	Remember,hope is a good thing!
root@se7en-LIFEBOOK-LH531:~/learn/Cpp_Program# 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值