zoukankan      html  css  js  c++  java
  • 第二十一章流 4文件的输入和输出 简单

    // 第二十一章流 4文件的输入和输出
    // 1输出数据到文件
       //(1)包含fstream头文件
       //(2)建立ofstream对像  如: ofstream ocout
       //(3)将对像与文件关联 ocout.open('123.txt');
       //(4)该对像可看作cout对像,因此我们使用该对像将数据输出到文件中,而不是屏幕中 ocout<<"abc";
       //(5)关闭与文件的连接 count.close();
    /*#include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
    	ofstream ofstr;
    	ofstr.open("123.txt");
    	ofstr<<"hello word";
    	ofstr.close();
        return 0;
    }*/
    
    //2 读取文件中的数据
    /*#include <iostream>
    #include <fstream>
    #include <ostream>
    using namespace std;
    int main()
    {
    	char temp[25] = {0};
    	ifstream fin("123.txt");
    	ofstream fout("124.txt");
    	fin>>temp; //读取
    	fout<<temp<<endl; //写入
    	fout<<temp<<endl;
    	fin.close();
    	fout.close();
    
    	cout<<"文件内容为:"<<temp<<endl;
    
        return 0;
    }*/
    
    // 4读取空格及空格后面的字符
    /*#include <iostream>
    #include <fstream>
    
    using namespace std;
    int main()
    {
    	const int num = 255;
    	char temp1[num]={0};
    	char temp2[num]={0};
    	ofstream fout("1.txt");
    	cout<<"请输入:"<<endl;
    	cin.getline(temp1,num-1,0);
    	temp1[num-1]='\0';
    	fout<<temp1;
    	fout.close();
    
    	ifstream fin("1.txt"); //读取
    	fin.getline(temp2,num-1,0); //读取内容
    	fout.open("2.txt"); //打开2  
    	fout<<temp2;        //写入到2中
    	cout<<temp2<<endl;  //输出
    	fout.close();       //关闭
        return 0;
    }*/
    

      

  • 相关阅读:
    指针数组和数组指针
    initializer_list
    main:处理命令行选项
    Synchronized 和Lock区别
    sleep和wait的区别
    什么时候会发生类初始化
    类的加载与ClassLoader的理解
    获取Class类的实例
    元注解
    IO流思维导图
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2709696.html
Copyright © 2011-2022 走看看