print_it program

This is a discussion on print_it program within the C Programming forums, part of the General Programming Boards category; print_it is a program that i have got from teach yourself c in 21 days. it supposed to print a ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    print_it program

    print_it is a program that i have got from teach yourself c in 21 days. it supposed to print a program listing with line numbers. but instead of printing with everything alligned to the left, everything is severly indented. it is so indented that it cannot print the whole listing because the listing just spreads out to the right. here is an example of what i mean:

    1: #include <stdio.
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2: blah blah blah
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3: blah blah blah

    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 );
    }
    also, when i change all of the stdprn's to stdout's, the listing is printed just the way it should be on the screen. any ideas? thanks
    Last edited by zack787; 01-15-2006 at 11:23 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > fprintf( stdprn, "%4d:\t%s", line++, buffer );
    This is where you get to understand what 'line feed' and 'carriage return' really mean
    Line feed just moves down one line, carriage return means move back to the left of the paper.

    Try
    Code:
    fprintf( stdprn, "%4d:\t%s\r", line++, buffer );
    fgets() internally strips off whatever the line control of the OS is (say CRLF for windows) and replaces it with the ANSI-C standard LF (only).

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    it worked! thanks a bunch!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21