16 August, 2009

Text File reader

Program which reads text (*.txt) files.

Source code:

/*Program to read text files*/


int main ()
char c;
char ch;
FILE *fp; /*File pointer*/
char fname[67];
printf ("This reader can read text files (*.txt) files only\n");
while (1) /*So that program continues to run untill terminated by user*/
printf ("\n\nEnter the file name (with or without path)\n");
gets (fname); /*Get the file name as a string*/
printf ("\n\n\n");
fp = fopen (fname, "r"); /*Opens the file*/
if (fp == NULL)
printf ("\nFile does not exist");
while (2) /*Reads characters from the text file*/
ch = getc (fp);
if (ch == EOF)
printf ("%c", ch);
fclose (fp); /*Closes the file*/
printf ("\n\n\n\nRead another file: Y/N\n\n");
c = getch ();
if ((c=='n')||(c=='N'))
exit (1);
getch ();

Download link for program

No comments: