• c++读写文件


    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
        using namespace std;
        string filename;
    
        cout << "Enter name of new file:" << endl;
        cin >> filename;
        
        //type 1
        //ofstream fout(filename.c_str());
        
        //type 2
        ofstream fout;
        fout.open(filename.c_str()); //如果一个文件名被申明为“string”,那么就必须使用 “c_str”,然而,当你申明一个文件名为字符数组型,就没有必要使用/
    
        fout << "this is my test of file read and write 
    " ;
        fout.close();
    
        //**********read*****************//
        ifstream fin(filename.c_str());
        char ch;
        // type 1
        //while (fin.get(ch))    //每次读取一个字符到ch
        //    cout << ch <<endl;
        
        //type 2
        fin >> ch;    // 读取一个字符 t
        string str;
        fin >> str; //读取 this
        cout << str << endl;
    
        //type 3
        //char ch2[100];
        //fin.getline(ch2,100);
        //int i = 0;
        //while(i<40)
        //{
        //    cout << "ch2[" << i << "]:" <<ch2[i]<< endl;
        //    i++;
        //}
        
        //type 4
        //string buff;
        //getline(fin,buff);
        //cout<<buff<<"
    
    ";
        //cout << "Done
    ";
    
        return 0;
    }
  • 相关阅读:
    ps 命令
    wc 命令
    grep 命令
    date 命令
    cal 命令
    df 命令
    secureCrt 开启Linux上的oracle服务
    动态规划_01背包问题_Java实现
    贪心算法_01背包问题_Java实现
    贪心算法_01背包问题_Java实现
  • 原文地址:https://www.cnblogs.com/shaogang/p/6785897.html
Copyright © 2020-2023  润新知