文件流

在C++中ofstream和istream提供了文件的输出和输入;这两个文件在头文件<fstream>定义

输出文件流和其他输出流不一样的一点就是:输出文件流的构造函数接受两个参数,第一个是文件名,第二个就是打开文件的模式,默认的模式是写文件(ios_base::out)这个模式从文件的开头写文件,改写任何数据;下表是第二个参数的类型:

第二个参数类型
常量说明
ios_base::app打开文件,在每一次写文件之前,移到文件末端
ios_base::ate打开文件,打开以后立即移到文件末端
ios_base::binary以二进制的模式进行文件的输入输出操作
ios_base::in  打开文件,从头开始读取文件内容
ios_base::out打开文件,从头开始写文件内容,覆盖已有的文件内容
ios_base::trunc打开文件并删除所有的已有数据

 

 

 

 

 

 

 

 

#include <iostream>
#include <fstream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) 
{
    std::ofstream outFile("text.txt",std::ios_base::trunc);
    if(!outFile.good())
    {
        std::cerr << "Error while opening file!" << std::endl;
    }
    
    outFile << "There were " << argc << "argument to this program." << std::endl;
    outFile << "There are : " << std::endl;
    for(int i = 0;i < argc;++i)
    {
        outFile << argv[i] << std::endl; 
    }
    
//    std::cout << argv[0] << std::endl;     //C:\Users\Administrator\Desktop\initializer_list\项目1.exe 
    //argv[0]这是储存文件路径 
    return 0;
}

 

转载于:https://www.cnblogs.com/boost/p/10356679.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值