Originally Posted by
jimblumberg
Post your modified code. Also show how these variables were defined.
Jim
Code:
// Read text from filenames entered by user OR text input from user and
// sort in alphabetical format.
#include <stdio.h>
#include <string.h>
#define SIZE 255
#define LEN 255
void namesort ( char name[LEN][SIZE], int last );
void showSortingResult ( char name[LEN][SIZE], int last );
// Function main begins program execution
int main(int argc, char * argv[])
{
char name[LEN][SIZE];
char secondname[LEN][SIZE];
int i,j=0, x=0; // indices of array
int f; // file counter
int last; // index of last item in array
FILE *fpPtr; // fpPtr is pointer
// if there is a filename from commandline
if ( argc > 1 ) {
for (f=1; f<=argc-1; f++){
/* This part for file input verification
if(f==1)
printf("%s\n",argv[1]);
else
printf("%s\n",argv[2]); */
// fopen opens file, Read text from a filename entered by user
fpPtr = fopen ( argv[f], "r" );
// Read from a file entered by user
while(fgets( name[j], 255, fpPtr )!=NULL){
printf( "Name %s\n", name[j] ); // Verfication
strcpy( secondname[x], name[j] );
printf( "Second name %s\n", secondname[x] ); // Verification
j++;
x++;
}
fclose( fpPtr ); // Closes the file
} // end for
// Sent to namesorting Function Prototype to sort by symbol, A-Z. a-z
printf("\n---- Sorting Process ---- \n"); //Verification
namesort ( name, j-1 );
// Output result using Function prototype
printf("\n---- Display the result ---- \n"); //Verification
showSortingResult ( name, j-1 );
printf("\n---- End of file input part ---- \n");//Verification
} // end if
else {
// Get manually entered data from user
fpPtr = stdin;
for(j = 0 ; !feof( fpPtr ) ; j++ ){
// Read next record
fgets( name[j], 255, fpPtr );
}
// Sent to namesorting Function Prototype to sort by symbol, A-Z. a-z
printf("\n---- Sorting Process ---- \n"); //Verification
namesort ( name, j );
// Output result using Function prototype
printf("\n---- Display the result ---- \n"); //Verification
showSortingResult ( name, j );
printf("\n---- End of manually input part ---- \n"); //Verification
} // end if else
return 0;
} // End Main
// Function to Sorting by symbol, A-Z, a-z
void namesort ( char name[LEN][SIZE], int last ){
int i, j, x; // indices of array
char *tokenPtr; // create char pointer;
char tempsort[LEN][SIZE];
for (i = last ; i > 0 ; i--){
for (j = 0 ; j <= last ; j++){
printf("sorting %s",name[j]); // For checking
for (x = 0 ; x <= last ; x++){
tokenPtr = strtok (name[j], ".");
while (tokenPtr != NULL){
printf("tokenPtr: %s\n", tokenPtr) ; // For checking
strcpy( tempsort[x], tokenPtr ); // assint tokenPtr to tempsort
printf("tempsort: %s\n", tempsort[x]) ; // For checking
tokenPtr = strtok ( NULL, "."); // get next token
} // end while
// printf("%s",name[j]) ; // For checking
}
}
}
} // End function prototype
// Function to Printf result
void showSortingResult ( char name[LEN][SIZE], int last ){
int i; // indices of array
// Write array to output
for (i = 0 ; i <= last ; i++)
printf("%s",name[i]) ; // For checking
} // End function prototype