Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Recommended Posts

Hello all. I think I'm making a newbie mistake. I'm trying to use the DoFileOpen function to open up a mastercam X file. I'm reading the help on it in Core/MFile_CH.h. It says that the variable it calls "filen" "MUST be complete path to file". So I wrote this:

 

LPCTSTR filen = "C:Program Filesmcamxchook_testsOpen_FileIGES_1.mcx";

 

Then in main I wrote this:

 

DoFileOpen(filen, merge, astext, checkUnits);

 

While debugging it says there's a bad pointer:

 

filen 0xcccccccc const char *

 

As I said, I think I'm making a stupid newbie mistake. After searching online for uses of LPCTSTR, I just got more confused. Any help is appreciated. Thanks in advance.

Link to comment
Share on other sites

You didn't allocate memory for your string. LPCTSTR refers to a string on the heap (dynamically allocated memory), so such assignment/allocation isn't going to work. The string type you've chosen isn't the best for what you're doing.

 

There's quite a few ways to allocate a string, for instance:

code:

char s[] = "yadayadayada";  //simple ASCII

TCHAR s1[] = _T("string"); //handles UNICODE too

char * st = new char[10];

strcpy(st, "yelp!"); //deprecated

string tempString; //stl::string

tempString = "temp"; //allows assignment

CString mfcString = "Microsoft uses these";

 

try something more like:

 
char filen[] = "C:yadayadayada.mcx"[/indent]

or

[indent]code:


TCHAR filen[] = _T("c:...");

[/indent]

which would put it on the stack instead (handling allocation for you)

 

or even (cutting a step)

[indent]code:


DoFileOpen("C:yadafile.txt", /*more arguments*/);

[/indent]

cutting the entire allocation problem altogether.

 

Remember also, you may need to double up on the char, because it's used as an escape character. E.g. "C:mcamxchooksmychook.dll"

 

Here's a link to a more complete tutorial on the subject:

Tutorial

Link to comment
Share on other sites

Here's a decent primer on the std::string class, which is what you want to end up using. It's far more powerfull than a simple character array:

 

String Class

 

Pay particular attention to these two entries:

/*Character array to string*/ s = ch; //Converts character array ch into string s.

/*String to character array*/ cp = s.c_str(); //Pointer cp points to a character array with the same characters as s.

 

Conversion comes up a lot, and typecasting isn't always the best idea when using these

 

what you'd end up with is code like this:

code:

#include <string>

using namespace std;

 

//... skipping stuff...

string filen = "C:mcamxdirfile.mcx";

DoFileOpen(filen.c_str(), /*args*/);

Link to comment
Share on other sites

Thanks for tutorial on the string class. I'll read through that to figure out it's benefits. I was able to open up the file that I wanted using DoFileOpen, with simply:

 

char filen[]= "C:Program Filesmcamxchook_testsOpen_FileIGES_1.mcx";

 

It's great info, and I'm sure I'll need the more robus string class at some point soon.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...