-
October 14th, 2012, 02:26 PM
#1
How to open file to read array and print words ending in "ed" to file.
Good day, new kid on the block here.
I need to command a user to open file for reading and then take the words
ending in "ed" and print them back to the file.
My problem is I understand opening a file, and comparing the strings but
putting them together so that I am writing the new items to the file
just boggles my poor soul.
So a few issues here: can't actually open the text file, and also not sure how to go about reading the array into it.
I appreciate all of your help, and please do not give me short replies so as to assume I know exactly what you mean because I am a sincere virgin to c++. THX
HTML Code:
#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//
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|