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


    头文件

    >#include<cstdio>
    >#include<cmath>
    >#include<cstring>
    >#include<cstdlib>
    >#include<algorithm>
    >#include<iostream>
    >#include<string>
    >#include<fstream>
    >using namespace std;
    


    **读取代码段** ####使用*ifstream* 然后对C++进行文件的读取####
    int main() {
        string buffer, filepath = "test.txt";
        ifstream myfile(filepath.c_str());
        //等同于 ifstream myfile("test.txt");
    
        while(getline(myfile, buffer)) {
            cout << buffer << endl;
        }
        myfile.close();
        return 0;
    }
    


    使用ofstream 然后对C++进行文件的写入####

    写入代码段

    int main() {
        string filepath = "test.txt";
        //ofstream myfile("test.txt");
        ofstream myfile(filepath.c_str());
    
        myfile << "hbhh" << endl;
        myfile << "aaaaa" << endl;
        myfile.close();
        return 0;
    }
    


  • 相关阅读:
    说说移动端web开发中的点击穿透问题
    将博客搬至CSDN
    IIS(4)
    IIS(2)
    IIS(3)
    IIS(1)
    链表
    常用到的关键字
    进程与线程
    文件系统的原理
  • 原文地址:https://www.cnblogs.com/smuzoey/p/11539766.html
Copyright © 2011-2022 走看看