1

I'm trying to learn some C and have chosen to use the book "SAMS Teach yourself C in 21 days". ( btw is there any good C book at all? This is my third! I always end up with bad or broken code that is supposed to work and hit a wall in my learning process when it doesn't! :-( ) Sadly I've run upon the code underneath that I just should type in and run. The typing went well but the running... well, not so well! The compiler gives me an error about this line:

{
printf ("Printer busy or disconnected\n"); error_handler; }

when I try to run this code. And since I'm VERY MUCH a novice when it comes to C coding, I have NO IDEA at all what to do, when the editor returns an error messages like this:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
rm -f -r build/Debug
rm -f dist/Debug/GNU-MacOSX/type_and_run

CLEAN SUCCESSFUL (total time: 158ms)
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/type_and_run
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/Type and run 1.o.d
gcc    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/Type and run 1.o.d -o build/Debug
/GNU-MacOSX/Type\ and\ run\ 1.o Type\ and\ run\ 1.c
i686-apple-darwin10-gcc-4.2.1: and: No such file or directory
i686-apple-darwin10-gcc-4.2.1: run: No such file or directory
i686-apple-darwin10-gcc-4.2.1: 1.o.d: No such file or directory
Type and run 1.c: In function 'do_heading':
Type and run 1.c:54: error: 'error_handler' undeclared (first use in this function)
Type and run 1.c:54: error: (Each undeclared identifier is reported only once
Type and run 1.c:54: error: for each function it appears in.)
make[2]: *** [build/Debug/GNU-MacOSX/Type and run 1.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 158ms)

All I can understand is that "error_handler" is undeclared and that it does not seem to be a library function. SIGH!


So said short, is there anything I can do to modify the code so it will work as intended and I can move on?

Or should I just throw the book over in the pile of useless books about C?

Here is the code in its full length:

void do_heading(char *filename);

int line = 0, page = 0;

int main(int argv, char *argc[])

{
    char buffer[256];
    FILE *fp;

    if (argv < 2) 
    {
        fprintf(stderr, "\nProper Usage is: " );
        fprintf(stderr, "\n\nprint_it filename.ext\n");
        return (1);
    }

    if ((fp = fopen(argc[1], "r")) == NULL) 
    {
        fprintf(stderr, "Error opening file, %s!", argc[1]);
        return (1);
    }

    page = 0;
    line = 1;
    do_heading(argc[1]);

    while( fgets( buffer, 256, fp ) != NULL ) 
    {
        if( line % 55 == 0 )
            do_heading(argc[1]);

        fprintf( stdout , "%4d:\t%s", line++, buffer );
    }

    fprintf( stdout , "\f" );
    fclose(fp);
    return 0;
}

void do_heading(char *filename) 
{
    page++;

    if (page > 1)
        fprintf( stdout , "\f");

    fprintf( stdout, "Page: %d, %s\n\n", page, filename);

    if ((stdout = fopen("PRT:","w")) == NULL) {
    printf ("Printer busy or disconnected\n"); error_handler; }

}
ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • 3
    "int argv" and "char *argc[]" ..really?!!! – Sangeeth Saravanaraj Dec 20 '11 at 17:17
  • But none the less the rest of the code okay except this line: { printf ("Printer busy or disconnected\n"); error_handler; } – Kim Merrild Dec 20 '11 at 17:20
  • 1
    The usual naming convention for variable length command line arguments for main is "int argc" and "char * argv[]" (or "const char * argv[]"). argc = argument count (which is the number of command line arguments) and argv = argument vector (array of pointers to char which points to the incoming command line arguments). So any book which mentions this convention wrongly must be tagged as useless C books! – Sangeeth Saravanaraj Dec 20 '11 at 17:20
  • Allright! Down the chute with "SAMS Teach yourself C in 21 days"... – Kim Merrild Dec 20 '11 at 17:22
  • Anysuggestions for a book that will teach me some C? – Kim Merrild Dec 20 '11 at 17:23
  • See the [Definitive C Book List](http://stackoverflow.com/q/562303/10077) – Fred Larson Dec 20 '11 at 17:25
  • OK I guess in your case, the error_handler could be a macro (#define - multiple lines) which just prints the error, (take appropriate action) and exit. This could have been defined in a different file and the author is expecting the reader to include that file! Could you please refer +/- 1 or 2 pages for any instruction on how to use the error_handler?! – Sangeeth Saravanaraj Dec 20 '11 at 17:27
  • Not familiar with that book, or what they may be building up to. If that is the program in its entirety, that "error_handler;" (along with quite a bit of the rest) can be removed. – That Chuck Guy Dec 20 '11 at 17:28
  • I've tried to remove that part Chuck… but with no luck! – Kim Merrild Dec 20 '11 at 17:41
  • Sangeeth do you mean on what page I found this code? – Kim Merrild Dec 20 '11 at 17:41
  • Thanks for the list Fred! Very usefull! :-) – Kim Merrild Dec 20 '11 at 17:42
  • 1
    I guess that author meant to say that you need to have error handler which will handle failure of `fopen` call. Just badly worded I guess! Please check out SO link posted by @Fred Larson – another.anon.coward Dec 20 '11 at 17:44
  • I've already done that and am chasing some pdf's :-> – Kim Merrild Dec 20 '11 at 17:45
  • The phrase "and run ..." is an instruction to you, not a command for you to type literally. The book *should* distinguish clearly, perhaps using typefaces, between English text and literal commands. And please double-check the book's definition of `main`; does it *really* declare `argv` followed by `argc`, or did you misread it? (Either is possible.) – Keith Thompson Dec 20 '11 at 19:12

2 Answers2

0

Try this book:

C Programming Language (2nd Edition) by Brian W. Kernighan, Dennis M. Ritchie.

You have argv and argc swapped.

if (argv 1)

Should be:

if (argc == 1)

You can simply remove the error_handler chunk as it seems to serve no purpose and is not declared in the code. What the book was likely trying to tell you was to fprintf to stderror perhaps?

Brett McLain
  • 2,000
  • 2
  • 14
  • 32
0

So the compiler output might look intimidating, but, as a beginner, the bit you care about is usually in English. This example is no exception:

Type and run 1.c: In function 'do_heading':
Type and run 1.c:54: error: 'error_handler' undeclared (first use in this function)
Type and run 1.c:54: error: (Each undeclared identifier is reported only once
Type and run 1.c:54: error: for each function it appears in.)

It's telling you that error_handler is undeclared. It appears nowhere else in your program so there's no way you're able to use it here. Let's look at the line:

if ((stdout = fopen("PRT:","w")) == NULL) {
    printf ("Printer busy or disconnected\n"); error_handler; }

You should know that semicolons are one way to indicate the end of a statement and that good practice is to put just one statement on a line. Break it up:

if ((stdout = fopen("PRT:","w")) == NULL) {
    printf ("Printer busy or disconnected\n"); 
    error_handler; 
}

Now it's obvious: you're just saying error_handler;; it's not doing anything, it's not a function call (no parentheses), not a variable you're assigning to or reading from so it's clearly a huge fat error. Delete it and try again.

By the way, if the line appeared in the book exactly as you've put it here - with two semicolons on a line - then yes, you should throw it on the fire. Read Kernighan and Ritchie's "The C Programming Language" and do all the exercises in it - you will be amazed how quickly you go from knowing nothing to being ready to work on real programs.

Tim
  • 5,024
  • 2
  • 30
  • 58
  • Hi guys Got caugth up with other to-do's yesterday but I WILL CERTAINLY return to you! Merry Christmas to you all! – Kim Merrild Dec 21 '11 at 12:55