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

 

C++ write and read file via fstream in ios::out,ios::in,ios::app mode

 

上一篇:rem实现移动端完全自适应,1:1还原设计图纸


下一篇:Ker f 是 G 的正规子群的完整证明