Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Mac > BOOK: Beginning Mac OS X Programming
|
BOOK: Beginning Mac OS X Programming
This is the forum to discuss the Wrox book Beginning Mac OS X Programming by Michael Trent, Drew McCormack; ISBN: 9780764573996
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Mac OS X Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2005, 11:46 AM
Registered User
 
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grepper tutorial - searching a file

The "Try It Out" - Searching a File under chapter 6, page 191 shows the code for "Grepper".
According to the book, when the program is run, it should open a text file, loop thru it line by line, and find the search word...
When I run the program my run log shows the following message:
Grepper has exited due to signal 10 (SIGBUS).

Anybody know what this means?

I have checked and double checked the code - used the file downloaded from the web-site - but this message persists! I suspect that the error is in the argument passed to the variable inpPath. This is in the Executable Info window under the "Arguments" tab.

Thanks,
Joseph
 
Old November 19th, 2005, 04:42 PM
Wrox Author
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Grepper has exited due to signal 10 (SIGBUS).

Anybody know what this means?
It means your program has crashed. If you run your program in Xcode's Debugger Xcode will tell you exactly which line of code is causing the crash. Often, but not always, the real error is somewhere near by.

So, run the program in the debugger, find out where it's crashing, and see if that jogs your memory. If you're still stuck, give us a little more information in the forums here and we'll figure it out together.

 
Old November 21st, 2005, 12:11 AM
Registered User
 
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.

I ran the program through the debugger as you suggested and it showed me the line that caused the crash, but otherwise it didn't much help. I had already realized that the file path to the text file which was passed to the variable via the info window was probably an incorrect path. It was typed as /Desktop/poem.rtf. After moving the poem.rtf file to the same folder as the saved grepper project and changing the path to ../poem.rtf did it finally work properly.

Joseph
 
Old December 3rd, 2005, 12:48 PM
Wrox Author
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah, I see the problem.

If the file does not exist, inpFile will be assigned to NULL on line 23. The debugger will break on line 28 when we pass a NULL FILE * into fgets. The moral of this story is do error checking. One kinda lazy way of doing this is to change inpFile assignment to:

Code:
FILE *inpFile = fopen(inpPath, "r");
if (!inpFile) {
    fprintf(stderr, "could not open %s\n", inpPath);
    return 1;
}
Of course, good error handling and program flow control requires lots of practice. This little fix gets the job done, but it's not very descriptive and it interrupts program flow. People spending more time writing command-line tools might look into things like the perror() function, the errno variable, and such.

The other moral of the story is learning how to recognize a crash when you see one. In this case the bug was the value of a variable was wrong. When the debugger stops on a line of code, first look at that line of code. Does it look right? If it does, then look at the value of every variable involved in that like. In this case you would notice inpFile had a suspicious value (0) and do more digging. Similarly, stepping through programs line by line and watching how the variables change can help find these kinds of things.






Similar Threads
Thread Thread Starter Forum Replies Last Post
searching a text file for repeated numbers Un33k Visual Basic 2005 Basics 1 October 30th, 2007 01:50 PM
Searching an expression in a text file. rachellim C# 2005 1 January 15th, 2007 11:18 AM
Searching the Windows File System pillowtest BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 0 July 16th, 2006 03:24 PM
Tutorial sithius Access 1 March 13th, 2006 03:44 PM
Tutorial surendran ASP.NET 1.0 and 1.1 Basics 11 March 29th, 2005 01:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.