Opening Files

Opening Files

Anonymous
Not applicable
356 Views
2 Replies
Message 1 of 3

Opening Files

Anonymous
Not applicable
I'm writing an arx program that asks a user for a file input, and then
opens the file they select from a dialog box. For some reason though,
my program seems to get messed up with the filename after the user
selects the file. That is, sometimes when I select the file, it reports
it's trying to open a filename which is blank, or garbled. But if I try
to open the file several times in a row, it will work again.

Any Clues?

Much Appreciated
-Jacob

bool GetFile( /* inout */ ::ifstream& inFile,
/* in */ char *msg)
{

char *fileName;

// AutoCAD specific file open dialog control
const char* title = msg; // Dialog box title
const char* defaultd = "/work/"; // Default directory
struct resbuf *fname; // Result buffer

fname = acutNewRb(RTSTR);

if(acedGetFileD(title, defaultd, "LIN", 16, fname) == RTNORM)
{
fileName = fname->resval.rstring;
}

// Release the result buffer
acutRelRb(fname);

// Now that the AutoCAD part is done, lets open the file.
acutPrintf("\n Attempting to open the file %s \n",fileName);

inFile.open(fileName);

if ( !inFile )
{
acutPrintf("\n *** Can't open input file *** \n");

delete fileName;
return false;
}

// Report success and delete our filename pointer.
acutPrintf("\n File Opened Successfully \n");
delete fileName;
return true;
}
0 Likes
357 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
I'm not familiar with the "ifstream" object, so i can't comment on whether
or not that's causing any problems. The one thing that i can say off the
top of my head, however, is that you continue to execute the function even
if acedGetFileD returns something other than RTNORM. This means that the
fileName may or may not be NULL. Since you don't initialize the pointer
fileName to NULL, it may come back as a garbled string.

-Rich


"Jacob Feindt" wrote in message
news:6B7BA91C4CC7693AD73EFD754192C9A3@in.WebX.maYIadrTaRb...
> I'm writing an arx program that asks a user for a file input, and then
> opens the file they select from a dialog box. For some reason though,
> my program seems to get messed up with the filename after the user
> selects the file. That is, sometimes when I select the file, it reports
> it's trying to open a filename which is blank, or garbled. But if I try
> to open the file several times in a row, it will work again.
>
> Any Clues?
>
> Much Appreciated
> -Jacob
>
> bool GetFile( /* inout */ ::ifstream& inFile,
> /* in */ char *msg)
> {
>
> char *fileName;
>
> // AutoCAD specific file open dialog control
> const char* title = msg; // Dialog box title
> const char* defaultd = "/work/"; // Default directory
> struct resbuf *fname; // Result buffer
>
> fname = acutNewRb(RTSTR);
>
> if(acedGetFileD(title, defaultd, "LIN", 16, fname) == RTNORM)
> {
> fileName = fname->resval.rstring;
> }
>
> // Release the result buffer
> acutRelRb(fname);
>
> // Now that the AutoCAD part is done, lets open the file.
> acutPrintf("\n Attempting to open the file %s \n",fileName);
>
> inFile.open(fileName);
>
> if ( !inFile )
> {
> acutPrintf("\n *** Can't open input file *** \n");
>
> delete fileName;
> return false;
> }
>
> // Report success and delete our filename pointer.
> acutPrintf("\n File Opened Successfully \n");
> delete fileName;
> return true;
> }
>
0 Likes
Message 3 of 3

Anonymous
Not applicable
Jacob:

You are assigning the returned string to 'filename', then releasing its
memory with acutRelRb(). Since you have a 'delete filename' later in your
function, I assume you intended to copy the string as in
'filename=strdup(fname->resval.rstring)'. However, a more efficient
approach that doesn't require additional memory to be allocated would be to
hijack the string from the result buffer:

filename = fname->resval.rstring;
fname->resval.rstring = NULL;
...
acutDelString(filename); //instead of delete
--
Owen Wengerd
President, ManuSoft ==> http://www.manusoft.com
VP Americas, CADLock, Inc. ==> http://www.cadlock.com
0 Likes