Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5283
Number of posts: 16725

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Newbie programming error? Posted by DarwinX86 on 18 Nov 2007 at 4:09 AM
Hi all, so I literally picked up a book (actually several) and decided to learn C today and I can't figure out what is wrong with the source code of a program the book just had me write.

My problem is this: the program is supposed to take the source code of other files and print them to the screen with with line numbers for the lines of code. The binary I produced executes, but when I try using the program by typing at a blank command prompt for example C:...> print_it hello.cpp, all I get is Page 1, hello.cpp and then a Windows button telling me that print_it has encountered an error and needs to close.

I thought that the problem might be with how I copied the source code from the book into my editor, but I have double-checked that, so I thought I'd post it here and see if anyone can help me.

Thanks,

/* 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, "%4:\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 );
}



Report
Re: Newbie programming error? Posted by HackmanC on 18 Nov 2007 at 5:18 PM
Just a typo in your coding...
Check the red "d" in there...

/* 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 ); // <-- HERE
}

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 );
}


Check how fprintf() works, in your book.

Good luck!
Hackman



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.