zoukankan      html  css  js  c++  java
  • C++ write and read file via fstream in ios::out,ios::in,ios::app mode

    #include <iostream>
    #include <uuid/uuid.h>
    #include <ostream>
    #include <istream>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    void retrieveUuid(char *uuidValue);
    void writeFile2();
    void readFile3();
    
    int main()
    {
        writeFile2(); 
        readFile3();
        return 0;
    }
    
    void readFile3()
    {
        fstream wFile;
        wFile.open("log3.txt",ios::app|ios::in|ios::out);
        if(!wFile.is_open())
        {
            cout<<"Create or open log3.txt failed!"<<endl;
        }
    
        string uuidValue;
        int num=0;
        while(getline(wFile,uuidValue))
        { 
            cout<<"Id="<<++num<<",value="<<uuidValue<<endl;
        } 
        wFile.close();
        printf("Finished!\n");
    }
    
    void writeFile2()
    {
        fstream wFile;
        wFile.open("log3.txt",ios::app|ios::out|ios::in);
        if(!wFile.is_open())
        {
            cout<<"Create or open log3.txt failed!"<<endl;
        }
    
        char *uuidValue=(char*)malloc(40);
        for(int i=0;i<10000;i++)
        {
            retrieveUuid(uuidValue);
            wFile<<uuidValue<<endl;
        }
        free(uuidValue);   
        wFile.close(); 
    }
    
    void retrieveUuid(char *uuidValue)
    {
        uuid_t newUUID;
        uuid_generate(newUUID);
        uuid_unparse(newUUID,uuidValue);
    }

    Complile and run

    g++ -g -std=c++2a h2.cpp -o h2 -luuid

    Run the ./h2 command

    ./h2

  • 相关阅读:
    2101 可达性统计
    POJ1179 Polygon
    POJ1015 Jury Compromise
    读入输出优化
    队列优化dijsktra(SPFA)的玄学优化
    5104 I-country
    CH5102 Mobile Service
    P1005 矩阵取数游戏
    (模板)线段树2
    POJ3666 Making the Grade
  • 原文地址:https://www.cnblogs.com/Fred1987/p/15679666.html
Copyright © 2011-2022 走看看