Good Day all!
Looking for a little help here.
I have to open a file containing a few words, some of which ending in "ed"
and print the only the words ending in "ed" back to file.
Here is what I have so far. I cannot seem to open the file, and also need to understand
how to read the string into the file correctly.
I seem to be missing the linkage between opening the file and reading the array into it.
Thanks for helping my poor sould.
Please refrain from short responses, in which you assume I know exactly what you are talking about.
I am hardly a nerd and far from a c++ guru. SOMEDAY.
#include <stdio.h>
#include <string.h>
int main( void )
{
int i; /* loop counter */
int length; /* length of current string */
char array[ 5 ][ 20 ] = {0}; /* 5 strings from user */
char filename [100];
FILE* fileptr;
printf ("Please enter the file to open:");
scanf_s ("%s", filename);
fileptr = fopen (filename, "r");
if (fileptr == NULL)
{
printf ("Unable to open '%s' for input. \n", filename);
}
else
{
while (!feof(fileptr))
/* read in 5 strings from user */
for ( i = 0; i <= 4; i++ ) {
printf( "\nThe strings ending with \"ED\" are:\n" );
}
/* loop through 5 strings */
for ( i = 0; i <= 4; i++ ) {
/* find length of current string */
length = strlen( &array[ i ][ 0 ] );
/* print string if it ends with "ED" */
if ( strcmp( &array[ i ][ length - 2 ], "ED" ) == 0 ) {
printf( "%s\n", &array[ i ][ 0 ] );
fclose (fileptr);
} /* end if */
}//end for //
} //end else//
return 0;
} //end main//