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!