twitter
    Find out what I'm doing, Follow Me :)

How To Use A Variable Filename

Load and display the file anyfile.c for an example of reading from any file. This program asks the user for the filename desired, reads in the filename and opens that file for reading. The entire file is then read and displayed on the monitor. It should pose no problems to your understanding so no additional comments will be made.


#include "stdio.h"
main( )
{
FILE *fp1;
char oneword[100],filename[25];
char *c;f
printf("enter filename -> ");
scanf("%s",filename); /* read the desired filename */
fp1 = fopen(filename,"r");
do {
c = fgets(oneword,100,fp1); /* get one line from the file */
if (c != NULL)
printf("%s",oneword); /* display it on the monitor */
} while (c != NULL); /* repeat until NULL */
fclose(fp1);
}



Compile and run this program.
When it requests a filename, enter the name and extension of any text file available, even one of the example C programs.