Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mac > BOOK: Beginning Mac OS X Programming
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 16th, 2005, 11:46 AM
Registered User
 
Join Date: Nov 2005
Location: , , .
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
  #2 (permalink)  
Old November 19th, 2005, 04:42 PM
Wrox Author
 
Join Date: Nov 2005
Location: , , .
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.

  #3 (permalink)  
Old November 21st, 2005, 12:11 AM
Registered User
 
Join Date: Nov 2005
Location: , , .
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
  #4 (permalink)  
Old December 3rd, 2005, 12:48 PM
Wrox Author
 
Join Date: Nov 2005
Location: , , .
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.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:35 AM.


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