zoukankan      html  css  js  c++  java
  • C++实现读写文件

    读文件:

    <fstream.h>//在标准C++中,已经使用取代< fstream.h>,所有的C++标准头文件都是无后缀的。
    int main()
    {
        ifstream fin(“input.txt”); //打开input.txt文件
        int number;
        float real;
        char letter, word[8];
        fin >> number; fin >> word; fin >> real; fin >> letter; 
        //也可以把这四行读取文件的代码写为更简单的一行。
        fin >> number >> word >> real >> letter;
        //它是如何运作的呢? 文件的每个空白之后, “>>” 操作符会停止读取内容, 直到遇到另一个>>操作符. 因为我们读取的每一行都被换行符分割开(是空白字符)
        return 0;  
    }

    写文件:

    <fstream.h>//在标准C++中,已经使用取代< fstream.h>,所有的C++标准头文件都是无后缀的。
    int main()
    {
        ofstream fout(“output.txt”); //打开ouput.txt文件
        char word[8];
        fout.write(word, 8);//写入值
        fout.close();//关闭文件  
        return 0;  
    }

    C读写文件:

    TODO

  • 相关阅读:
    python 读写文件
    python之创建文件写入内容
    python之生成随机密码
    python实例七
    python 实例六
    python 实例四
    python实例二
    python实例一
    【BZOJ】1610: [Usaco2008 Feb]Line连线游戏
    【BZOJ】1602:[Usaco2008 Oct]牧场行走
  • 原文地址:https://www.cnblogs.com/2018shawn/p/14983261.html
Copyright © 2011-2022 走看看