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

I recently added some code to read data from a text file into my chook. I added these lines:

 

File * angles;

angles = fopen("C:...axis_alignment.txt", "rt");

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

fclose (angles);

 

Then later in the code while using the rotate_silent function I added the variable alpha into the rotation angle input.

 

After rebuilding the dll I ran it but it gave me a "Debug Assertion Failed" error saying that on Line 54 of fscanf.c there is an error:

 

Expression: stream !=NULL

 

I had the chook working with a numeric value for the rotation angle input. I have included the screenshot of the error on the FTP site into the "All pictures..." folder. It's called fscanf_error.bmp.

 

Can someone please let me know if this is a bug and how I can fix it.

Link to comment
Share on other sites

The file is a text file stream as you can see from the code above. I'm not sure why that would not be valid. The error gave me a line number that was blank in my code and referred to "fscanf.c".

 

When you say my file is not open, do you mean I need to open up the text file with additional code in my chook like the "OpenFile()" or "DoFileOpen()" commands?

Link to comment
Share on other sites

Thanks for the input Roger, but I don't understand the line:

 

*Is angles non-NULL at this point?*

 

It gave me an error when I compiled with this code in there. I'm a newbie, and don't quite understand the language yet. Let me know what I'm doing wrong.

Link to comment
Share on other sites

No, you do't add that text.

What you need to find out is "that" true.

 

My guess (and this is only a guess) is that the 'fopen' is failing to open the specified file ->

angles = fopen("C:...axis_alignment.txt", "rt");

If that is the case, then 'angles' (your FILE*) will be NULL, which will cause an error to occur on the ->

fscanf (angles, "%lf", α); line.

Does the 'fscanf' line happen to be "line 54" in your file?

 

One way to discover this would be to add this code between the 'fopen' and 'fscanf' lines ->

if (angles == NULL)

{

::MessageBox(NULL, "Failed to open file", "Error!", MB_OK);

}

 

You should always check items such as FILE* to make sure they are valid before trying to use them.

Link to comment
Share on other sites

Roger. Thanks for the post. I didn't think that was supposed to be code. I'm glad you included the MessageBox code, that what I needed since I can't use "printf".

 

As I was mentioning in other posts, the line 54 error was blank in my code, didn't correspond to the fopen or fscanf line, and referred to "fscanf.c" which I learned was a standard library file. I will try your suggestion out this afternoon. I appreciate the help thus far cheers.gif

Link to comment
Share on other sites

Thanks again for the MessageBox code. I was able to get it to show that it wasn't reading from my text file. But again, I double checked and line 54 was a blank line in my code. I really believe that it is a problem using the fscanf.c file.

 

I'm also following the post that you and Jure are discussing so that I can also get debugging to work with Visual Studio and Mastercam X.

 

I can't find any other errors in my code, and keep getting the line 54 error even after I move my code around. I'm afraid I'll have to try another method of reading a text file. Please let me know if there is anything else I can do to debug this error, thanks.

Link to comment
Share on other sites

quote:

After rebuilding the dll I ran it but it gave me a "Debug Assertion Failed" error saying that on Line 54 of fscanf.c there is an error:

This is telling you that the "error" was tiggered at line:54 in fscanf.c and that this was an 'Assertion Failure'

 

Looking in fscanf.c (not that this is nessesary, since we know the real error is in "our code", not fscanf.c) at that line, it contains this code -> _ASSERTE(stream != NULL);

 

This is basiclly the telling you that opening the file failed, because the "file pointer" is NULL), which is a bad thing... ;(

 

Rest assured - the issue is in your code.

Either the file you are specifying...

Does not exist.

-or-

"How" you are specifying the pathname in the call to fopen does not resolve to "pointing to" a real file. (possibly a "bad" path).

Link to comment
Share on other sites

Hello Roger. Thanks for explaining further, that is what I was trying to understand about the error I was getting.

 

Since then I have taken out the path and placed the text file in the same location as my main.cpp. Even though I had a colleague recheck my path, we did not see any error, and did not receive any warnings or errors after compiling. I then copied the code into a separate program so that I could debug the text file read section and it worked without any errors and printed my text document's values. Then I also wrote another set of code here:

 

alpha = 0.0f;

ifstream myfile ("axis_alignment.txt");

if (myfile.is_open())

{

while (! myfile.eof() )

{

myfile>>alpha;

}

}

myfile.close();

 

This also compiled without errors. I then tried both methods of reading from a text file in my chook. With the text file placed in my main.cpp directory, both worked without errors. But neither method took the variable "alpha" when i called it from the rotate_silent command.

 

Again, I appreciate your help and patience.

Link to comment
Share on other sites

Sure thing. I hope I'm not swamping you with my multiple questions on different threads.

 

I'm going to stick with the method above of using the "ifstream" method to read from a text file instead of the fopen method.

 

As I run that Chook it does not give me an error that it can't read from the file. So the read method seems to be working. However I'm storing the text file's data in the variable called alpha. Then I use alpha as my input rotation angle in the function rotate_silent. Sorry about the confusing post.

 

If it's easier, or I still don't make sense I'd be glad to email you my main.cpp file in the format that works best for you. Thanks again.

Link to comment
Share on other sites

Roger. I must not be operating this debugger like I'm supposed to be. Here are the steps I take:

 

1. Edit code, place breakpoints

2. Rebuild to delete old .dll

3. Select Debug ->Start (F5)

4. Mastercam (the debugger .exe) opens

5. Open a file to be run a Chook on.

6. Run the Chook (which rotates the part)

7. Return to VS to look at the Debug window

8. Debug window shows that the Chook was run

9. The Output and Auto windows are blank

10. My Watch of variables a and alpha report the error as before.

 

I think one of my steps is incorrect. I appreciate your corrections.

 

I have run the above steps with the sample code in your message above, and the execution did not stop, it simply performed the chook.

Link to comment
Share on other sites

After Step #5 when you start your C-Hook it should run up to your Breakpoint and stop.

 

3 images...

BreakPoints.jpg

1> Is how the BreakPoint looks (in VS 2005) prior to F5 (Start Debug).

 

2> After F5 and X is starting.

If you hover your mouse in the "open" Breakpoint circle VS will tell you ---

"The breakpoint will not currently be hit. No symbols have been loaded for this document"

This is expected as you have not really started to debug your CHook code yet.

 

3> Once you've start you CHook (by having X run it), then the BreakPoints will go back to 'filled circles' to let you know that they are *active*. And when a Breakpoint is *hit*, execution of your code will stop at that line and the yellow arrow will appear in the currently hit Breakpoint.

Link to comment
Share on other sites

1. This isn't a problem with the code per say, but the design. Your alpha variable has passed out of scope.

 

Hence: CXX0017: Error: symbol "alpha" not found

 

2. fopen doesn't return a file stream, it returns a FILE *, which aren't the same.

 

FILE *'s are an old C way of doing things, and not really encouraged in C++

 

3. The reason we're not seeing the error is that you're grabbing a very small chunk of code, where the error is outside it.

 

Let us take a look at your readtext function

code:

void readtext()

{

alpha = 0.0f;

ifstream myfile ("axis_alignment.txt");

if (myfile.is_open())

{

myfile >> alpha;

myfile.close();

a = alpha;

}

else cout << "Unable to open file";

}

You're missing the variable list, so m_main isn't going to see the contents of alpha, because they're local to readtext's portion of the stack.

 

try something a little more like this

 

code:

#include <string>

#include <iostream>

using namespace std;

 

// m_main goes here....

// m_main()

// ... excerpted code...

//.....

 

string readtext (ifstream & in)

{

string alpha

in.open("axis_alignment.txt");

if (in.is_open())

{

in>> alpha;

in.close();

return alpha;

}

else cerr<< "Unable to open file";

}

this would give you a call on the order of

code:

ifstream myfile;

string data = readtext(myfile);

cout<< "My data is: "<<data<<endl;

Link to comment
Share on other sites

First, let me say thanks for the continued help and suggestions. Second, let me clarify, I'm a mechanical engineer, with very little programming experience. I'm just trying to get a few different Chooks together to help automate a research project for my Major Professor.

 

I placed your code in my chook after I closed m_main. Then inside m_main I initialzed alpha to be 0 along with changing it's type (in all locations) to be double.

 

It compiled but with one warning:

 

" warning C4715: 'readtext' : not all control paths return a value"

 

And after running the Chook it simply ran with the initialized value of 0.

 

I'm not sure what you mean by "You're missing the variable list, so m_main isn't going to see the contents of alpha,..." I used this syntax inside m_main to try and call the function readtext:

 

readtext()

 

As I said I'm new to programming and hobbling along as you can tell. But I'm guessing that when you return alpha from readtext outside of m_main, that m_main will take that in. But with the error I'm getting I don't think it's reading alpha into m_main.

 

Furthermore, I'm still trying to get debugging to work with Visual Studio. And I cannot tell whether the file was even read because I don't see a result from this line:

 

else cerr<< "Unable to open file";

 

Thanks for the help again, and please let me know what is confusing in my reply.

Link to comment
Share on other sites

Let me clarify, when I said I used:

 

readtext() that was when I was working with it before. I have since taken that out with the latest code suggestion. I thought if I'd call readtext from m_main, that it would pass the results of alpha along with it.

Link to comment
Share on other sites

Arenner,

 

 

" warning C4715: 'readtext' : not all control paths return a value" is because I typed in the code without attempting to compile it, and left out a line to handle the last case. The final line of read text should have been

code:

  return "Error";

Not seeing any result from the cerr line is good. It means the line was never called, and that the file opened correctly.

 

To clarify a little c++, the word return means "go back to the place that this function was called from, and continue to execute there. Give the guy that called me this data." It marks an exit from a subroutine (function).

 

I would strongly recommend getting either a short book on c++ or an online tutorial. The time you spend reading it to get the theoreticals down you will save a thousand times over in debugging.

 

I found this tutorial online thanks to google, and gave it a quick look over. It seems like a decent primer for you:

 

C++ Tutorial

 

That being said, if you want to skip the primer, this page contains most of what you'll need to work with files:

Files Page

 

Unfortunately, there really is too much for me to be able to walk you through this entirely, but feel free to keep up with the questions.

 

Oh, as for the red circle with the question, that can happen for two reasons. First is that you put it on a line of code that isn't subject to breaking. Test for this by putting in more of them. If they all don't work, it isn't that. The other issue is that the symbols weren't loaded, or can't be found. Try hitting f5. If they still don't work, it's because you're not getting the mastercam.exe to run in debug mode. Roger has a thread in this forum where he's walking someone through that. Check there.

 

Best of luck,

Link to comment
Share on other sites

Thanks Jon. I'll add that return to readtext. I'm always wondering what is a valid entry after return. For instance one of the Chooks I'm using has a return of "MC_NOERROR".

 

I had to chuckle when I clicked on the links you gave me, because I was actually using the same exact site for reference. Glad to know I was using a good one. Also I had just picked up a reference book this weekend.

 

I'll continue to work on debugging my program, thanks.

Link to comment
Share on other sites

I had to switch your suggestion of return "Error"; to return 0; because my function type is double. I'm trying to get the function call based on the examples in the tutorial I'm using. Here is my function call:

 

readtext(alpha);

 

Based on your description of return, I placed the readtext function above m_main in my code, and wrote the call as above trying to pass the returned value of alpha into m_main. But it failed rebuild because it can't convert alpha from 'double' to 'std::ifstream &'. So either I'm committing a very newbie mistake, or the ifstream lib function can't handle reading a double.

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