Member Avatar

I just started learning C with a book called "Sams | Teach Yourself C in 21 Days, 6th edition" and I've already run into a problem I can't solve. The book gives a program that is supposed to display any code from any saved source file - including its own. As the book states on page 37, "The list_it.c program in Listing 2.2 displays C program listings that you have saved. These listings are displayed on the screen with line numbers added."

The problem is that I don't know how to use it to make it work and the book doesn't give any specific instructions for using it. What am I supposed to do with it? I'm using Dev C++ and when I run it all I get is this:

            Proper Usage is:

            list_it filename.ext

I've tried renaming and recompliling this program with two different filenames - list_it multiply.c and list_it list_it.c and it just gave me scrambled junk. I've also tried opening multiply.c and using the same approach and I got the same result.
Here's the code:

/* list_it.c - This program displays a listing with line numbers! */

#include <stdio.h>
#include <stdlib.h>

void display_usage(void);
int line;

int main( int argc, char *argv[] )
    char buffer[256];
    FILE *fp;

    if(argc < 2)
        return 1;

    if (( fp = fopen( argv[1], "r" )) == NULL )

        fprintf( stderr, "Error opening file, %s!", argv[1] );


    line = 1;

    while( fgets( buffer, 256, fp ) != NULL )
        fprintf( stdout, "%4d:\t%s", line++, buffer );

    return 0;


void display_usage(void)
    fprintf(stderr, "\nProper Usage is: " );
    fprintf(stderr, "\n\nlist_it filename.ext\n" );

Recommended Answers

All 5 Replies

You use it like it says.

nlist_it filename.ext

Run it with the name of a text file as a parameter and it will list that file to the console. I compiled and ran it and it works as it is supposed to.

By the way, no book will teach you a language in 21 days.

commented: Or how to use the command line or what the command line is and all you can command on the command line. ;) +15
Member Avatar

Yes, it was supposed to be run from command after all. But, I forgot about navigating directories with cd and other commands that I learned a million moons ago. The first few times I tried it in the console, it just told me no such command exists. Both of you were very helpful. Thank you. My question now, is there a way to run it from Dev C++ or does it only work from inside the console?

I only used Dev C++ briefly so I can't recall if it allowed you to specify which directory to run in but I will bet you can specify command line parameters. Not one IDE I used in decades didn't have that. The early ones didn't have a pause after execution but that was added later. So go looking around your IDE for the options you need here. It appears there's really nothing busted here as the code looked fine and Reverend Jim confirmed that.

it just told me no such command exists

You'd either have to:

  1. Run it from the folder containing the executable
  2. Run it from another folder but fully qualify the executable
  3. Place the executable in a folder contained in your PATH environment variable

I have a folder namedf D:\Utils and have added it to PATH. That way any custom command line apps I write or download can be placed there and accessible from anywhere.

Member Avatar

Thanks, Jim. Prior to your last comment I had actually made it work in the console. I'm not sure if you got that from my previous comment or not. I just had to type "cd Documents" on the command line then when the next line popped up, I typed in "list_it list_it.c" and hit return which printed out the code in the console. I did it with another short C program as well. Before you and rproffitt helped me, I was pretty clueless and not sure how or where exactly the code was supposed to run. Now I know that it's in command. As proffitt suggested, I'll have to root around in Dev C++ to see if there's a way to run the code from inside the IDE instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.