Thread: Cannot Execute/Run Program

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Cannot Execute/Run Program

    Hi everyone,

    I am new to the board and not really a programmer yet but I found this book SAMS Teach Yourself C in 21 Days by Bradley Jones and Peter Aitken, so I decided to just get my head around it. I would like to have some knowledge on C somehow as I may need it in my profession in the future.

    The problem is with the first "Type and Run," where the code looks like this:

    Code:
    /* print_it.c--This program prints a listing with line numbers! */ 
    #include <stdlib.h> 
    #include <stdio.h> 
     
    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( stdprn, "%4d:\t%s", line++, buffer ); 
      } 
     
      fprintf( stdprn, "\f" ); 
      fclose(fp); 
      return 0; 
    } 
     
    void do_heading( char *filename ) 
    { 
       page++; 
     
       if ( page > 1) 
          fprintf( stdprn, "\f" ); 
     
       fprintf( stdprn, "Page: %d, %s\n\n", page, filename ); 
    }
    I am using the gcc compiler in Ubuntu 12.04 LTS and I get the following error:

    Code:
    print_it.c: In function ‘main’:
    print_it.c:36:15: error: ‘stdprn’ undeclared (first use in this function)
    print_it.c:36:15: note: each undeclared identifier is reported only once for each function it appears in
    print_it.c: In function ‘do_heading’:
    print_it.c:49:16: error: ‘stdprn’ undeclared (first use in this function)
    I was told that "stdprn" can be recognised by a DOS based compiler and the book says I can try using "stdout" instead. It looks like this now:

    Code:
    /* print_it.c--This program prints a listing with line numbers! */ 
    #include <stdlib.h> 
    #include <stdio.h> 
     
    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 ); 
    }
    It compiled OK with the gcc compiler but I only get this when I run the program:

    Code:
    Proper Usage is: 
    
    print_it filename.ext
    I am not sure whether I should continue looking into this but even when I tried compiling and running it on Windows, the .exe file won't even launch. The other ones do but this first one doesn't.

    Questions:

    1. What should be done to make this program run?
    2. Even though the book says "don't care" if the reader does not understand the items (It's Day 1/Lesson 1), I would still like it to run as I don't want to experience compiling and running problems in the future. Should I even bother doing this section of the book or is it obsolete and should be skipped?

    Thanks.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Your program displays that error message when you forget to enter a command line argument.

    Command Line Arguments in C - Cprogramming.com

    Read your text carefully to see if it tells your what stdprn is - You'll probably find that it is something along the lines of stdout
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Hmmm. I haven't read that part yet in the book as I am still in Chapter 1. I just copied and pasted the code from the examples as the the book would like the readers to try compiling and running first.

    If it's copied directly and does not run as it is, there should be something wrong with it, then.

    I may have to read through all the chapters and get back to this first "Type & Run." Or if anyone can help me just get this program to run, it would be very much appreciated. Thanks.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It appears that stdprn is a console stream for a printer.

    It is a very old technique and should be avoided.

    Quote Originally Posted by http://support.microsoft.com
    This method works only in the MS-DOS operating system because the "stdprn" stream is not defined by Microsoft Windows or Microsoft Windows NT.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Your print_it program is telling you that it expects the name of a file as an argument (standard Usage message). Open a terminal, go to the directory where print_it is and enter ./print_it print_it.c - assuming your source file is named print_it.c

    Or in Windows, go to folder, Shift-right-click the containing folder and select "Open command window here". Then enter print_it print_it.c

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by ntdvs View Post
    If it's copied directly and does not run as it is, there should be something wrong with it, then.
    There's nothing wrong with it. You have to give it the name of the filename to list. For example, if you called your program print_it.exe on windows and you want to use it to list your source code then you would type

    print_it.exe print_it.c

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    I got it working now. Thanks, guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program won't execute
    By rayne117 in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2009, 07:16 AM
  2. Replies: 0
    Last Post: 04-08-2009, 04:23 PM
  3. Execute another program
    By Revan-th in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2006, 09:53 PM
  4. Can't execute another program
    By dwks in forum C Programming
    Replies: 7
    Last Post: 08-07-2005, 12:38 PM
  5. Cannot Execute a C++ Program
    By Vivekasundaram in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 01:53 AM