• Write File


    Write to File with C++

    #include <iostream.h> 
    #include <fstream.h> 
    
    int main() 
    { 
        const char *FILENAME = "myfile.txt"; 
    
        ofstream fout(FILENAME); 
    
        cout << "Enter your text: "; 
        char str[100]; 
        cin >> str; 
        fout << "here is your text
    "; 
        fout << str << endl; 
    
        fout.close(); 
    
        ifstream fin(FILENAME); 
        char ch; 
        while (fin.get(ch)) {
            cout << ch; 
        }
        fin.close(); 
    
        return 0; 
    } 
    
    #include <algorithm>
    #include <cstdlib>
    #include <fstream>
    #include <functional>
    #include <iostream>
    #include <iterator>
    #include <vector>
    
    using namespace std;
    
    template <class T>
    void print(T & c){
        for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
            std::cout << *i << endl;
        }
    }
    
    int main()
    {
        vector <int> output_data(10);
    
        generate(output_data.begin(),output_data.end(),rand);
        transform(output_data.begin(),output_data.end(),
                output_data.begin(),bind2nd(modulus<int>(),10));
        print(output_data);
    
        ofstream out("data.txt");
    
        if(!out){
            cout << "Couldn't open output file
    ";
            return 0;
        }
    
        copy(output_data.begin(),output_data.end(),ostream_iterator<int>(out,"
    "));
        out.close();
    
        ifstream in("data.txt");
        if(!in){
            cout << "Couldn't open input file
    ";
            return 0;
        }
    
        vector<int> input_data((istream_iterator<int>(in)),istream_iterator<int>());
        in.close();
    
        print(input_data);
    
        return 0;
    }
    

    Write to File with Qt

    Write Binary to File with Qt

    
    void WriteBinaryToFile(QString binaryStr, QString filePath)
    {
        QFile file;
        QByteArray ba;
    
        QStringList ltStrs = binaryStr.split(' ');
        foreach(QString str,ltStrs) {
            ba.append((char)(str.toInt(0,16) & 0xff));
        }
    
        file.setFileName(filePath);
        if(!file.open(QIODevice::WriteOnly)){
            return;
        }
        
        file.write(ba);
        file.close();
    }
    

    Write plain Text to File with Qt

    void WritePlainTextToFile(QString plainText, QString filePath)
    {
        QFile file;
        QTextStream out;
    
        file.setFileName(qsFilePath);
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
            return;
        }
    
        out.setDevice(&file);
        out << plainText;
        file.close();
    }
    

    Write File with Unicode bom

    ///< some include
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QString str = QString("这是中文,QString");
        QFile file;
        QTextStream out;
    
        file.setFileName("a.txt");
        if(!file.open(QIODevice::WriteOnly|QIODevice::Text)){
            qDebug() << file.errorString();
            return 0;
        }
    
        out.setDevice(&file);
        out.setCodec("UTF-16");   ///< unicode
        out.setGenerateByteOrderMark(true); ///< with bom
        out << str;
        file.close();
        qDebug() << "OK!";
    
        return a.exec();
    }
    

    How to check(see) it in vim

     vim see the file hex: %!xxd
     see the text    : %!xxd -r
    

    Another way to write file

    ofstream myfile;
    myfile.open("a.txt");
    myfile << "xEFxBBxBF"; // UTF-8 BOM
    myfile << "xE2x98xBB"; // U+263B
    myfile.close();
    
    ofstream myfile;
    myfile.open("a.txt");
    myfile << "xFFxFE"; // UTF-16 BOM
    myfile << "x3Bx26"; // U+263B
    myfile.close();
    

  • 相关阅读:
    新的开始 和一些总结
    常用物流快递单号查询API接口对接net源码示例_快递鸟
    从SpringBoot到SpringCloudAlibaba简明教程(一):初识SpringBoot及其基础项目构建
    Debug > python list.sort()食用方法
    Debug > python统计列表中元素个数
    Debug > python中的True False 0 1
    java获取本机外网ipV4
    aop统一处理修改请求参数
    [算法] 国家地区数组按照中文首字母排序
    js转换ASCII码
  • 原文地址:https://www.cnblogs.com/zi-xing/p/7725572.html
Copyright © 2020-2023  润新知