C++ ofstream write content to file,ifstream read content from file

#include <iostream>
#include <ctime>
#include <uuid/uuid.h>
#include <unistd.h>
#include "Model/BookStruct.cpp"
#include <string.h>
#include <typeinfo>
#include <ostream>
#include <fstream>
#include <sstream>

using namespace std;
char *uuidValue = (char *)malloc(40);
char *dtValue = (char *)malloc(20);

char *getUuid1()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID, uuidValue);
    return uuidValue;
}

void readFile10()
{
    string fileName="log.txt";
    ifstream rFile;
    rFile.open(fileName,std::ios_base::in);
    if(!rFile.is_open())
    {
        cout<<"Open log.txt failed!"<<endl;
        return;
    }

    string line;
    int num=0;
    while(getline(rFile,line))
    {
        if(++num%1000000==0)
        {
            cout<<"Num="<<num<<",line="<<line<<",now is "<<getTime7()<<endl;
        }
    }

    rFile.close();
    free(uuidValue);
    free(dtValue);
    cout<<"Finished in readFil10()"<<endl;
}

void logFile9()
{
    string fileName = "log.txt";
    ofstream wFile;
    wFile.open(fileName, std::ios_base::app);
    if (!wFile.is_open())
    {
        cout << "Create log.txt failed!" << endl;
        return;
    }

    int loops=100,repeatedTimes=1000000;
    int num=0;
    stringstream ss;
    for(int i=0;i<loops;i++)
    {
        for(int j=0;j<repeatedTimes;j++)
        {
            ss<<++num<<","<<getUuid1()<<endl;
        }
        wFile<<ss.str();
        ss=stringstream();
        cout<<"Num="<<num<<",now is "<<getTime7()<<endl;
    }

    wFile.close();
    free(uuidValue);
    free(dtValue);
    cout<<"Finished in logFile9()"<<endl;
}

 

posted @ 2022-02-05 21:30  FredGrit  阅读(32)  评论(0编辑  收藏  举报