Thread: Learning C in 21 Days

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    Learning C in 21 Days

    Hey all,

    First post to the forum. I am trying to learn C and I am really excited, however, I wonder if my choice of text was bad. I picked up Sams Learn C in 21 days and I am trying to compile the print_it program. I am getting all kinds of errors that point to stdprn. Apparently, stdprn is defined for a Windows/Dos environment and I am using Linux.

    I did google this and tried to fix the issue, only being met with other errors and I am in over my head. Here is the original .c program(Below are modifications I tried):

    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);
      }
    Here is where I try to define stdprn, only met with more errors:

    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[] )
    
    FILE *stdprn = popen("/usr/bin/lpr", "w");
    
    {
      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);
      }
    How would I go about getting this resolved?

    Thanks!!!

    Joe

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well its always good to post the exact error and warning messages. Otherwise we have to check it all manually, with no idea of where to start.

    Luckily, it seems the problem (or at least one of them!) is this
    Code:
    int main( int argv, char *argc[] )
    
    FILE *stdprn = popen("/usr/bin/lpr", "w");
    
    {
    You need to put that line inside "main". Basically move the "{" up to before the "FILE*" line.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Quote Originally Posted by nadroj View Post
    Well its always good to post the exact error and warning messages. Otherwise we have to check it all manually, with no idea of where to start.

    Luckily, it seems the problem (or at least one of them!) is this
    Code:
    int main( int argv, char *argc[] )
    
    FILE *stdprn = popen("/usr/bin/lpr", "w");
    
    {
    You need to put that line inside "main". Basically move the "{" up to before the "FILE*" line.
    Ok, sweet, allot less output, but still cryptic to me (For now )... Here is what I get...

    Code:
    cc print_it.c -o print_it
    print_it.c: In function ‘do_heading’:
    print_it.c:46: error: ‘stdprn’ undeclared (first use in this function)
    print_it.c:46: error: (Each undeclared identifier is reported only once
    print_it.c:46: error: for each function it appears in.)

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Well, I figured out the syntax, but was met with a seg fault. Apperently, its Windows centric and I cant use the text. I am just learning and don't know where to go with porting C to Linux :-D.

    I will close this thread and have opened another one asking for advice on other books I can get that is specific to Linux.

    Thanks so much for your help!!!

    Joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Plz help me C programm
    By ferroz1 in forum C Programming
    Replies: 7
    Last Post: 05-10-2008, 06:55 AM
  2. So you think you can learn C or C++ in 21 days?
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 07-23-2005, 02:50 PM
  3. Fun Learning a New Language
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-30-2003, 10:03 PM
  4. Blackjack (21) Game
    By Rabidpunk in forum Game Programming
    Replies: 12
    Last Post: 01-11-2002, 03:19 AM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 11:50 PM