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:

fopen, ifstream


Recommended Posts

I'm trying to open a text file and read its contents. Then I use the value in the text file as my "alpha".

 

I have implemented these methods:

 

1)

ifstream inFile("C:mcamxaxis_alignment.txt");

inFile>>alpha;

 

2)

FILE *instream;

instream=fopen("C:mcamxaxis_alignment.txt","r");

fscanf(instream,"%lf",α);

 

I put these two methods into m_main of the standard chook wizard. But when I run the chook it is not opening the file. I put an if(instream != NULL) and placed the function using alpha inside it, and the function is never performed. So I know that instream is NULL. But I cannot see why. Any help is appreciated.

Link to comment
Share on other sites

Thank you both for chiming in. I was hoping you two would be the ones to provide input.

 

1) I have checked, and yes the file exists, is a text file, and has read and write permissions (and is in that exact location). I also put it in the same folder as main just for good measure.

 

markov can you tell me what you mean by "errno" and where to find that? smile.gif I found a site to do GetLastError() and may try that.

 

2) As markov says MasterShake, I'm certain it isn't even reading the file yet because it's registering the stream as NULL. So I don't know if that will change anything:(

 

I could use a little more info on setlocale(), I'm not sure I understand how to debug with that?

 

Either way, thanks again guys.

Link to comment
Share on other sites

Oh, the other thing I forgot to ask, is there any reason I can't do this inside of m_main?

 

I use the wizard's m_main function:

 

extern "C" __declspec(dllexport) int m_main (int not_used)

 

So all of the code I mention above is inside that function. I don't think this would be a problem, but wanted to mention it just in case.

Link to comment
Share on other sites

errno is just an integer variable (or a macro that results in the _errno() function call) and gives you the error code of the last C runtime function call that failed. GetLastError() is a Win32 API that gives you the error code of the last failed system call. Just print the errors, probably using a MessageBox call, with something like this:

code:

MessageBox(..., strerror(errno), ...);

char s[100];

FormatMessage(..., GetLastError(), ... , s, ...);

MessageBox(..., s, ..);

Link to comment
Share on other sites

The sample from MSDN should work.

 

I have another idea. Are you by any chance compiling with VS2003, and creating C-hook for X2? If that is the case, you have a "complicated" C-hook, as described in the other thread. There may be a chance it would still work if you link it statically to the C runtime.

Link to comment
Share on other sites

Somewhere in the project properties (I am not at my computer now so I can not check) there is an option to select the runtime library. If it does not use a variation of the word "static", then it probably says "libcmt.lib".

 

Note that it is not advisable to run in the multiple runtime environments (which is what you are going to end up with if you statically link you C-hook) even if it appears to work (you may discover subtle problems later), but you probably do not have an option.

Link to comment
Share on other sites

Oh, yeah, that part should be simple. But that's just part of Chooks I'm developing to automate some positioning of CAD models in Mastercam with data generated from external programs.

 

I tried building with VS 2005. And didn't use fscanf or fopen, but I used ifstream, and it now is reading from my text file just fine. Thanks for your help, but I'm not sure what changed.

Link to comment
Share on other sites

quote:

Thanks for your help, but I'm not sure what changed.

A lot, namely the compiler and more importantly the header files! What was happening here is that you were (indirectly) using structure declarations from one version of the header files to access code (in the runtime DLL) that was compiled with a different version of the header files and different compiler.

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...