Program review

This is a discussion on Program review within the C Programming forums, part of the General Programming Boards category; Hey, I posted on here the other night with some problems I was having with a program I'm writing for ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    10

    Program review

    Hey, I posted on here the other night with some problems I was having with a program I'm writing for a Computer Science course. I've finally got it up and working but I feel like I could make it a lot more friendly looking.

    The purpose of this program is to accept text from a file, given through the command line, and reverse that text and print it out on the screen.

    I was wondering if anyone could show me how to make my code more efficient while keeping it alive.

    Here's my code:

    Code:
    /* Libraries */
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    
    int revString( char *src, FILE *fhold ); /* Prototype for fuction */
    
    int main ( int argc, char * argv[ ] ) 
    {
    
    	char inString [ 128 ];		/* holds string input from files 	   */
    	char numberLines [ 100 ];	/* used to number lines (option) 	   */
    	char fileName [ 50 ];		/* used to hold the name of the file 	   */	
    	FILE * inptr;			/* FILE ptr as defined in stdio.h          */
    	int i, j;                       /* counters for loops                      */
    	int lineNum = 1;		/* lineNum counts the number of lines	   */
    	int end = 0;			/* end quits the loop when file is scanned */
    	
    	/* The following checks the command line arguments */
    	/* to make sure they are valid and check for the   */
    	/* 		"-n" argument.			   */
    			
    	if( argc == 1 ) {
    		printf( "Please enter a filename. Syntax: ./rev \"-n option\" \"filename\"\n" );
    		return 0; /* Quit so nothing will break */
        	}
        	else if( argc == 2 )
        	{
    		strcpy( fileName, argv[ 1 ] );
        	}
        	else if ( argc == 3 )
        	{
    		strcpy( fileName, argv[ 2 ] );			/* Copy the name of the file from the cmd line	*/
    		strcpy( numberLines, argv[ 1 ] );       	/* Copy the second argument (better be "-n")	*/
                    if ( strcmp( numberLines, "-n" ) != 0 ) {	/* Use strcmp() to see if cmd arg is "-n"	*/
                        
                        printf( "Invalid Syntax: ./rev \"-n option\" \"filename\"\n" );
                        return 0; /* Quit so nothing will break */
    		}
        	}
        	else
        	{ 
    		printf( "Please enter a filename. Syntax: ./rev \"-n option\" \"filename\"\n" );
          		return 0;
        	}
    
    	/* Check to see if the file can be opened */
        	if ( ( inptr = fopen( fileName, "r" ) ) == NULL ) {
     		printf( "File could not be opened.\n" );
              	return 0; /* File cannot be opened so quit */
        	}
    
            /* Power house of the program. This while statement will */
            /* be in charge of scaning the file, numbering the lines */
            /*          , and printing the text in reverse.          */
    
            while( end != 1)
            {
                 end = revString( inString, inptr );
                 
                 /* Checking for EOF */
                 if( feof( inptr ) != 0)
                 {
                    end = 1;
                 }
    
                 /* Prints line number, if option is in use */
                 if( strcmp( numberLines, "-n" ) == 0) {
                    printf( "%d ", lineNum );
                 }
    
                 /* Begin scanning the file */
                 for( i = 0; inString[ i ] != '\0'; i++ );
                 --i;
    
                 /* If there is a newline char, we don't want it */
                 if( inString[ i ] == '\n') {
                    --i;
                 }
    
                /* Now the array is printed in reverse */
                while( i >= 0) {
                    printf("%c", inString[ i ]);
                    --i;
                }
    
                lineNum = lineNum + 1; /* 1, 2, 3... */
                printf("\n");
    
    
                for ( j = 0; j < 127; j++ ) {
                    inString[ j ] = '\0';
                }
            }
    
    	fclose( inptr ); /* Close the file when finished   */
    	return 0;	 /* returns int to make main happy */	
    } /* End main() */
    
    int revString( char *src, FILE * fhold )
    {
    
        if( fgets( src, 81, fhold ) != NULL ) {
                     
    
             if ( src[ 0 ] == '\n' ) {
                 return ( revString( src, fhold ) );
             }
             else {
                 return 0;
             }
         }
         else {
            return 1;
         }
            
    }
    Thanks!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) You should have posted this to your existing thread.
    2) You haven't fixed the problems that were already addressed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  4. review my telephone network simulation program
    By debuger2004 in forum C Programming
    Replies: 3
    Last Post: 06-20-2003, 02:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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