Reputation: 527
I need to modify code written in c++. The code opens a file authfile
and writes 49 bytes to it. In case the file already exists, I need to make sure the existing data will be overwritten.
The original code first deleted the file and then created a new file. For reasons beyond this discussion, I cannot delete the file, I only need to make sure it is empty, before i write new data to it.
Below is the function that writes the data. How can I modify it, so thatexisting content of the file will be overwritten ?
I suppose, I need to tchange the options of popen
bool Util::add_mcookie(const std::string &mcookie, const char *display,
const std::string &xauth_cmd, const std::string &authfile)
{
FILE *fp;
std::string cmd = xauth_cmd + " -f " + authfile + " -q";
fp = popen(cmd.c_str(), "w");
if (!fp)
return false;
fprintf(fp, "remove %s\n", display);
fprintf(fp, "add %s %s %s\n", display, ".", mcookie.c_str());
fprintf(fp, "exit\n");
pclose(fp);
return true;
}
Upvotes: 0
Views: 297
Reputation: 499
Change popen
to fopen
and pclose
to fclose
. popen
/ pclose
is for opening / closing a process. Also, it looks like you're creating the process with flags. You just need to give fopen
the file path
Upvotes: 3