Dave Coventry Dave Coventry - 28 days ago 16
C++ Question

Segmentation Fault on fprintf iofputs.c: No such file or directory

I have the following code:

FILE* fp;
if(value.empty())
return ID;
fp=fopen((tmpDir+"/"+filename).c_str(),"a");
string sID="";
if(ID>-1){
sID=to_string(ID)+"\t";
}
string row=sID+drawingID+"\t"+data+"\n";
fprintf(fp,"%s",row.c_str());
fclose(fp);


Under GDB, the code fails with the following errors:

Program received signal SIGSEGV, Segmentation fault.
__GI__IO_fputs (str=0x4a864 "1000001\t0\t70\t1\n", fp=0x0) at iofputs.c:38
38 iofputs.c: No such file or directory.

Answer Source

You did not check the result of fopen.

according to the error message fpis 0, meaning fopen has failed.

I would guess, the tmpDir is non-existent or non-accessable