• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
p-boost-q
博客园    首页    新随笔    联系   管理    订阅  订阅
文件流

在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;
}

 

posted on 2019-02-08 20:06  p-boost-q  阅读(157)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3