G
Gan Quan
I'm writing a c++ program that has many (100+) threads read/write files
simultaneously. It works well if not considering the efficiency. The
file i/o seems to be the bottleneck.
This is my code to read from and write to files:
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
bool write(const string &path, const string &contents,
ios:penmode mode)
{
ofstream out;
bool status;
out.open(path.c_str(), mode);
if (!out.fail()) {
out << contents;
}
status = !out.fail();
out.close();
return status;
}
bool read(const string &path, string &contents)
{
ifstream in;
stringstream ss;
bool status;
contents.clear();
in.open(path.c_str(), ios::in);
if (in) {
ss << in.rdbuf();
contents = ss.str();
}
status = !in.fail();
in.close();
return status;
}
I have few clues about how to optimize my code, any direction would be
greatly appreciated.
simultaneously. It works well if not considering the efficiency. The
file i/o seems to be the bottleneck.
This is my code to read from and write to files:
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
bool write(const string &path, const string &contents,
ios:penmode mode)
{
ofstream out;
bool status;
out.open(path.c_str(), mode);
if (!out.fail()) {
out << contents;
}
status = !out.fail();
out.close();
return status;
}
bool read(const string &path, string &contents)
{
ifstream in;
stringstream ss;
bool status;
contents.clear();
in.open(path.c_str(), ios::in);
if (in) {
ss << in.rdbuf();
contents = ss.str();
}
status = !in.fail();
in.close();
return status;
}
I have few clues about how to optimize my code, any direction would be
greatly appreciated.