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